Running out of disk space on a Linux VPS can lead to performance issues, failed installations, and even service downtime. In this guide, we will cover how to identify, clean, and optimize disk space to ensure smooth server operations.
1. Check Disk Usage
Before freeing up space, you need to determine what is consuming the most storage.
Check Overall Disk Space
df -h
This command displays the total, used, and available space on your system.
Find Large Directories
du -ah / | sort -rh | head -20
This command shows the 20 largest directories and files on your system.
2. Delete Unnecessary Log Files
Log files can accumulate over time, consuming significant disk space.
Find and Remove Large Log Files
sudo find /var/log -type f -name "*.log" -size +100M
sudo rm -rf /var/log/*.log
Clear Journal Logs
sudo journalctl --vacuum-size=100M
This reduces systemd logs to 100MB.
3. Clean Up Package Cache
Unused package files take up space and should be cleaned regularly.
For Debian/Ubuntu
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get clean
For CentOS/RHEL
sudo yum clean all
4. Remove Unused Docker Images and Containers
If you are running Docker, old images and containers may be using excessive disk space.
docker system prune -a
This removes unused containers, networks, and images.
5. Identify and Delete Large Files
Finding and removing unnecessary large files can free up space quickly.
Find Large Files
find / -type f -size +500M -exec ls -lh {} +
Delete Large Files
sudo rm -rf /path/to/largefile
6. Resize and Extend Disk Space
If cleaning up is not enough, consider resizing your disk.
Check Available Partitions
lsblk
Resize a Partition
For LVM-based systems:
sudo lvextend -l +100%FREE /dev/mapper/vg-name-lv-name
sudo resize2fs /dev/mapper/vg-name-lv-name
7. Use Cloud Storage for Large Data
Consider moving non-essential large files to cloud storage like AWS S3, Google Drive, or external block storage.
Conclusion
Regularly monitoring and maintaining disk space on your Linux VPS prevents crashes and ensures optimal performance. By following these steps, you can efficiently manage storage and avoid unexpected downtime.

Comments
Post a Comment