...

Top Linux Commands to Clean & Optimize Disk Space on Your VPS (Complete Guide)

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:

  1. Clear heavy log files
  2. Remove unused cache
  3. Clean old packages
  4. Remove temporary files
  5. Free gigabytes of disk space safely
  6. Check which folders are using storage
  7. 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 -h

To 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 -y

This 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.



                        

William Anderson

I am a versatile Full-Stack Web Developer with a strong focus on Laravel, Livewire, Vue.js, and Tailwind CSS. With extensive experience in backend development, I specialize in building scalable, efficient, and high-performance web applications.