Introduction / Why This Is Needed
Disk space on a Linux server or workstation can run out unexpectedly due to the accumulation of system files: package caches, old kernel versions, uncleared logs, and temporary data. This leads to write errors, service disruptions, and the inability to install updates. This guide will help you safely free up 5 to 50 GB (or more) in under 20 minutes, without affecting user data or disrupting system operation.
Requirements / Preparation
Before you begin, ensure:
- You have sudo privileges or root user access.
- The system is running one of the supported distributions: Ubuntu/Debian (apt), CentOS/RHEL/Fedora (dnf/yum).
- You have created a backup of important data (just in case).
- Basic utilities are installed:
ncdu(optional but useful),aptordnf.
Step 1: Analyzing Disk Usage
Before deleting anything, identify the "heavy" directories. This will help you target your cleanup efforts.
# Install ncdu for interactive analysis (Debian/Ubuntu)
sudo apt install ncdu
# Or use the built-in du command for a quick overview
sudo du -sh /* 2>/dev/null | sort -rh | head -20
The du -sh /* command will show the size of each root directory. Pay attention to /var, /usr, /home, and /tmp. For a more detailed analysis inside a specific folder:
ncdu /var/log
ncdu /usr
Important: Do not delete files in /usr and /etc without fully understanding their purpose.
Step 2: Cleaning the Package Manager Cache
Package managers store downloaded archive files (.deb, .rpm) in their cache. These files occupy space but are usually unnecessary after package installation.
For Debian/Ubuntu (apt):
# Clean the cache (deletes files from /var/cache/apt/archives)
sudo apt-get clean
# Clean only obsolete files (without removing current ones)
sudo apt-get autoclean
For CentOS/RHEL/Fedora (dnf/yum):
# Clean all cached metadata and packages
sudo dnf clean all
# For older systems (yum)
sudo yum clean all
Step 3: Removing Old Kernel Versions
Each kernel update leaves the old version on the disk. Usually, it's sufficient to keep the current kernel and one backup.
Debian/Ubuntu:
# List installed kernels
dpkg --list | grep linux-image
# Automatically remove old versions (keeps the last 2)
sudo apt-get autoremove --purge
# Or manually (example):
sudo apt-get purge linux-image-5.4.0-42-generic
CentOS/RHEL 8+/Fedora (dnf):
# List installed kernels
rpm -qa | grep kernel
# Remove all but the last two (requires package-cleanup)
sudo dnf install yum-utils # if not installed
sudo package-cleanup --oldkernels --count=2
⚠️ Important: Never delete the currently booted kernel. Check the active kernel with uname -r.
Step 4: Cleaning System Logs
Logs in /var/log can grow indefinitely. Additionally, systemd-journald stores binary logs.
Rotating and deleting old logs:
# View log sizes
sudo du -sh /var/log/*
# Delete compressed old logs (older than 30 days)
sudo find /var/log -type f -name "*.gz" -mtime +30 -delete
sudo find /var/log -type f -name "*.log.*" -mtime +30 -delete
# Clear active logs (caution! Check first)
sudo find /var/log -type f -name "*.log" -mtime +7 -exec truncate -s 0 {} \;
Cleaning journald (systemd journal):
# Check current journal size
journalctl --disk-usage
# Keep logs only for the last 7 days
sudo journalctl --vacuum-time=7d
# Or limit size to 500 MB
sudo journalctl --vacuum-size=500M
# Permanent limit (add to /etc/systemd/journald.conf):
# SystemMaxUse=500M
# MaxRetentionSec=1week
sudo systemctl restart systemd-journald
Step 5: Finding and Deleting Large Temporary Files
Temporary files in /tmp, ~/.cache, /var/tmp often remain after crashes.
# Find files >100 MB in /tmp and /var/tmp (older than 10 days)
sudo find /tmp /var/tmp -type f -size +100M -mtime +10 -exec ls -lh {} \;
# Delete them (check the list first!)
sudo find /tmp /var/tmp -type f -size +100M -mtime +10 -delete
# Clean user cache (e.g., for browsers, package managers)
rm -rf ~/.cache/*
# Clean pip/npm caches (if used)
rm -rf ~/.cache/pip
rm -rf ~/.npm
💡 Tip: For safe deletion, use -ls instead of -delete first to see what will be removed.
Checking the Result
After each step (or at the end), check free space:
df -h /
Or for a detailed analysis:
ncdu /
Ensure you have sufficient free space (at least 10-15% of total volume for stable operation). Also, verify that critical services are running:
systemctl status --failed
Potential Issues
1. "No space left on device" error when trying to delete
You might be deleting an open file (e.g., a log being written by a process). After deleting such a file, space is not freed until the file is closed.
Solution: Find the process via lsof | grep deleted and restart it. For example, sudo systemctl restart nginx.
2. System fails to boot after kernel removal
If you accidentally removed all kernels except the current one, and the current one is corrupted. Solution: Boot into rescue mode via GRUB and reinstall the kernel from a LiveCD.
3. Necessary files deleted in /var/log
If you truncated an active log that is still needed for debugging.
Solution: Recovery is impossible, but you can redirect logs to a new file: sudo service nginx restart (reopens the log). In the future, use truncate only on old files.
4. Not enough space to execute commands
Even simple commands (e.g., apt-get clean) require a minimum of free space.
Solution: Manually delete the largest files (via find in /tmp or /var/log) to free up 100-500 MB, then repeat the steps.
5. Permission errors
When deleting files in system directories without sudo.
Solution: Always use sudo for operations in /var, /usr, /opt. For user caches (~/.cache), no privileges are required.
Last updated: 2026-02-15. Tested on Ubuntu 22.04, Debian 12, CentOS Stream 9.