Struggling with low disk space on your VPS? Here is a complete guide to clean logs, remove unused files, clear cache, free several gigabytes, and keep your server running smoothly. Includes all essential Linux commands for disk optimization.
When your VPS starts running out of disk space, websites may slow down, services can fail unexpectedly, and
deployments may not work.
The good news?
Linux provides powerful commands to quickly clean and optimize
disk usage โ even on a small 20GB VPS.
In this blog, youโll learn all essential commands to:
- Clear heavy log files
- Remove unused cache
- Clean old packages
- Remove temporary files
- Free gigabytes of disk space safely
- Check which folders are using storage
- Letโs get started!
๐ 1. Check Total Disk Space
Before cleaning, check how much disk space your VPS is using.
df -h
This shows all mounted drives, including used and available space.
๐ 2. Find Big Directories on Your Server
To locate which folders are consuming heavy space:
sudo du -sh /* | sort -h
To check only /var (most heavy files are here):
sudo du -sh /var/* | sort -hTo analyze website folders:
sudo du -sh /var/www/*
This helps identify which sites or logs need cleanup.
๐๏ธ 3. Clean Linux System Logs (FREE 1โ2GB)
Linux system logs grow very fast and often consume gigabytes.
Reduce journald logs:
sudo journalctl --vacuum-size=100M
This limits logs to 100MB only.
Delete old rotated logs:
sudo rm -rf /var/log/*.gz
sudo rm -rf /var/log/*.[0-9]
sudo rm -rf /var/log/apt/*.gz
This instantly removes old archived logs
๐งน 4. Clear APT Cache
APT cache stores old packages that you donโt need.
sudo apt-get clean
sudo apt-get autoremove -yThis usually frees 100โ300MB.
๐ง 5. Remove Old Snap Packages (if Snap Installed)
Snap keeps multiple old versions and eats disk space.
sudo snap set system refresh.retain=2
sudo snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do sudo snap remove "$snapname" --revision="$revision"; done
This keeps only 2 versions and deletes old unused ones.
โก 6. Clean Temporary Files
Temp directories fill up without notice.
sudo rm -rf /tmp/*
sudo rm -rf /var/tmp/*
Safe to run anytime.
๐งญ 7. Remove Old Kernels
If your VPS is old, it may have many unused kernel versions.
sudo apt-get autoremove --purge -y
This removes outdated kernels safely.
๐๏ธ 8. Find Large Files Manually
Scan for files larger than 300MB:
sudo find / -type f -size +300M๐ฏ Final Thoughts
With these commands, you can easily free several gigabytes of disk space and keep your VPS running fast and stable.
This guide is perfect for:
Developers
Server admins
Website owners
Anyone managing a VPS with Ubuntu/Debian
Use these commands monthly or whenever you notice your VPS storage getting low.