sz

Sejan

Blog

Freeing Disk Space on a Linux VPS: Essential Commands, Safety Breakdown & Best Practices

Home › DevOps & Cloud › Freeing Disk Space on a Linux VPS: Essential Commands, Safety Breakdown & Best Practices
Freeing Disk Space on a Linux VPS: Essential Commands, Safety Breakdown & Best Practices

There are few alerts more concerning for a system administrator or web developer than receiving an emergency notification that a cloud VPS is at 98% disk capacity. When a Linux server runs out of disk space (No space left on device), databases crash, background cron jobs fail, SSH logins may become blocked, and web servers halt operations.

Fortunately, most Linux systems accumulate gigabytes of temporary data—including APT package caches, old kernel headers, rotating systemd logs, and dangling Docker layers—that can be safely pruned without rebooting or endangering live production services.

In this guide, we will break down the essential terminal commands to analyze your storage, categorize cleanup operations by their safety levels (Safe vs. Caution), and implement automated safeguards to prevent future storage emergencies.

Phase 1: Diagnose What Is Consuming Your Storage

Before deleting any files, you must accurately diagnose where the storage bloat is located.

1. Check Overall Disk Usage (df -h)

Run the disk free utility with human-readable sizing:

df -h

Look at the Use% column for the root partition (/). If usage is above 85–90%, immediate cleanup is recommended.

2. Check Inode Utilization (df -i)

Sometimes your server reports "disk full" even when df -h shows available gigabytes. This happens when the filesystem runs out of inodes (file index pointers), usually caused by millions of tiny session files or email queues.

df -i

If the IUse% column is at 100%, you need to find and delete directories containing mass small files rather than large binary files.

3. Identify the Heaviest Directories

Scan the top-level directories to find which specific folders are consuming the most space:

sudo du -sh /* 2>/dev/null | sort -hr | head -n 10

Common culprits include /var/log (runaway application logs), /var/lib/docker (unpruned container data), /var/cache (package manager archives), and /home (user backups).

Phase 2: 100% Safe Cleanup Commands (Green Light)

These commands can be safely executed on virtually any production Ubuntu, Debian, or Linux server without breaking running applications or services.

1. Clean Package Manager Caches (apt clean & apt autoclean)

Whenever you install or upgrade software using APT, Debian/Ubuntu keeps copies of the downloaded .deb package archives in /var/cache/apt/archives/. Over months of routine upgrades, this folder can easily swallow 2GB–8GB of disk space.

# Remove obsolete packages that can no longer be downloaded
sudo apt autoclean

# Remove all downloaded .deb package files completely (100% safe)
sudo apt clean

Safety Rating: 🟢 100% Safe. Running apt clean does not uninstall any software from your system; it only wipes the installer archive cache.

2. Remove Obsolete Packages and Unused Kernels (apt autoremove)

When software packages are removed or system kernels are updated, old dependencies and obsolete Linux kernel images remain installed on disk.

sudo apt autoremove --purge

Safety Rating: 🟢 Safe. The --purge flag ensures that leftover configuration files from uninstalled dependencies are wiped clean as well.

3. Vacuum Systemd Journal Logs (journalctl)

By default, systemd-journald stores system logs in /var/log/journal/. On active servers with heavy traffic, journal logs can grow unchecked to 5GB–15GB.

# Check current disk space consumed by journal logs
journalctl --disk-usage

# Retain only the last 3 days of logs
sudo journalctl --vacuum-time=3d

# Alternatively, restrict journal logs to a maximum of 200MB
sudo journalctl --vacuum-size=200M

Safety Rating: 🟢 100% Safe. Older logs are safely retired while current system events remain fully accessible.

4. Clear Old Temporary Files in /tmp

Applications frequently write scratch files to /tmp. Delete files that have not been accessed in over 7 days:

sudo find /tmp -type f -atime +7 -delete

Phase 3: Docker & Container Cleanup

If your VPS hosts Docker containers, old build caches and untagged images are often the primary cause of sudden disk starvation.

1. Prune Dangling Images & Build Cache

# Remove dangling (untagged) images and stopped containers
docker system prune

# Deep cleanup: Remove ALL unused images, stopped containers, and build cache
docker system prune -a

2. The Volume Warning: Be Careful with --volumes

Docker also allows adding the --volumes flag: docker system prune -a --volumes.

Safety Rating: 🟡 Caution! Never blindly run docker system prune --volumes if any of your persistent data (such as MySQL, PostgreSQL, or Redis databases) lives in anonymous or unattached Docker volumes. Only use this if you are certain all persistent data is mounted to explicit host directories.

Phase 4: Risky Cleanup Actions (Proceed with Care)

There are common cleanup mistakes that can disrupt running processes if executed incorrectly.

1. Never Delete Active Log Files with rm

If an active log file (such as /var/log/nginx/access.log or /var/log/syslog) reaches 20GB, deleting it with sudo rm /var/log/nginx/access.log is a common mistake. If the Nginx process still holds an open file descriptor, Linux will continue writing to disk space that is now invisible, and no disk space will be freed until Nginx is restarted.

Instead, truncate the log file to zero bytes in-place:

# Safe: Zero-out the file without breaking the file descriptor
sudo truncate -s 0 /var/log/nginx/access.log

# Or using shell redirection:
sudo : > /var/log/nginx/access.log

2. Finding Deleted but Still Open Files (lsof +L1)

If you deleted files with rm but df -h still shows 100% usage, check for unreleased file locks:

sudo lsof +L1

Restart the corresponding service (e.g., sudo systemctl restart nginx) to instantly release the allocated space.

Phase 5: Automated Maintenance Strategy

To avoid manual cleanup fire drills, configure proactive automatic limits on your VPS:

1. Cap Systemd Journal Space

Edit /etc/systemd/journald.conf and enforce a maximum storage ceiling:

[Journal]
SystemMaxUse=250M
SystemKeepFree=1G

Apply the change with: sudo systemctl restart systemd-journald.

2. Ensure logrotate is Active

Verify that logrotate runs daily by checking /etc/logrotate.d/. Ensure standard application logs are compressed with compress and deleted after 7–14 days.

Summary: Quick Command Cheat Sheet

  • Fastest immediate reclaim: sudo apt clean && sudo apt autoremove --purge
  • Tame runaway system logs: sudo journalctl --vacuum-size=200M
  • Wipe unused Docker caches: docker system prune -a
  • Truncate bloated log files: sudo truncate -s 0 /var/log/example.log

Following this structured approach ensures your Linux VPS maintains consistent free storage, optimal I/O throughput, and zero downtime.

Comments (0)

Leave a Reply ↓

No comments yet. Be the first to start the discussion!

Leave a Comment