Introduction / Why This Is Needed
Over time, temporary files, old packages, caches, and logs accumulate on a Linux disk, potentially consuming gigabytes of space. This leads to low disk space warnings, system slowdowns, and update issues. This guide will help you safely and efficiently free up disk space using both built-in utilities and manual methods. After completion, you'll reclaim 5-10 GB (and sometimes more) without deleting user data.
Prerequisites / Preparation
- Access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, Arch, etc.).
- Superuser privileges (
sudo) to clean system directories. - The
ncduutility installed (recommended but not required):sudo apt install ncdu(Debian/Ubuntu) orsudo dnf install ncdu(RHEL/Fedora). - Caution: Before deleting any file, ensure it is not in use by active processes. It is recommended to back up important data.
Step-by-Step Instructions
Step 1: Analyze Disk Usage
Before deleting anything, understand what is consuming space. Don't guess—use analysis tools.
- Quick overview of the root partition:
df -h
Shows usage of all mounted partitions. Pay attention to%Usefor/(root). - Deep analysis with
ncdu(recommended):sudo ncdu /
This is an interactive utility that slowly but very clearly shows which directories are the largest. Navigation: up/down arrows, Enter to enter a directory,dto delete (be careful!). Pressqto exit. - Alternative without installing
ncdu:sudo du -sh /* 2>/dev/null | sort -rh | head -20
This command shows the 20 largest directories in the root (/).2>/dev/nullhides access errors.
💡 Tip: Most often, the main "space eaters" are
/var(logs, package caches),/usr(installed programs),/home(user files), and/opt(third-party software).
Step 2: Clean Package Manager Cache
Package managers store downloaded package archives, which can be removed after installation.
- For Debian/Ubuntu (APT):
# Clean local repository of downloaded package files (cache) sudo apt clean # Remove packages installed as dependencies but no longer needed sudo apt autoremove --purge # (Optional) Clean old versions of installed packages (dangerous! only if you are sure) # sudo apt autoremove --purge --auto-remove--purgealso removes configuration files. Be careful: this may reset software settings. - For RHEL/CentOS/Fedora (DNF/YUM):
# Remove unused dependencies sudo dnf autoremove # Clean cache of all packages (downloaded rpm files) sudo dnf clean all - For Arch Linux (Pacman):
# Remove unused packages (dependencies that nothing requires) sudo pacman -Rns $(pacman -Qdtq) # Clean package cache (keeps only current versions) sudo paccache -r
Step 3: Remove Old Logs
Logs in /var/log can grow significantly, especially if there are system issues.
- Clean systemd journal (if using systemd):
# Show current journal disk usage journalctl --disk-usage # Clear logs older than 7 days sudo journalctl --vacuum-time=7d # Or clear, leaving no more than 500 MB # sudo journalctl --vacuum-size=500M - Clean traditional logs in
/var/log:# Navigate to the logs directory cd /var/log # Show large files (larger than 100 MB) sudo find . -type f -size +100M -exec ls -lh {} \; # Delete old compressed logs (e.g., .gz files older than 30 days) sudo find /var/log -name "*.gz" -type f -mtime +30 -delete # Delete old uncompressed logs (careful! better to review first) # sudo find /var/log -type f -name "*.log" -mtime +30 -delete
Do not randomly deletesyslog,messages,auth.logfiles—they may be needed for diagnostics. It's safer to delete only clearly old archives (.gz,.old,.1,.2).
Step 4: Clean Temporary Files
Temporary directories /tmp and ~/.cache often contain leftovers from installations and updates.
# Clean system /tmp (requires sudo)
sudo rm -rf /tmp/*
# Clean user cache (e.g., browsers, Flatpak, Snap)
rm -rf ~/.cache/*
# Clean /var/tmp (temporary files persisting across reboots)
sudo rm -rf /var/tmp/*
⚠️ Important: Ensure no active processes are writing to these directories. This is usually safe, but in rare cases, it might cause application failures. If in doubt, reboot the system before cleaning
/tmp.
Step 5: Find and Delete Large Unused Files
Search for personal or system files you haven't used in a long time.
- Find large files in the home directory:
# Find files larger than 500 MB in /home find /home -type f -size +500M -exec ls -lh {} \; - Find unused files (not accessed in over 90 days):
# Caution! This command can take a long time sudo find / -type f -size +100M -atime +90 -exec ls -lh {} \; 2>/dev/null
The-atime +90flag finds files not accessed in the last 90 days. Review the list and manually delete unnecessary items (e.g., old ISO images, logs, archives). - Find and remove old kernels (Debian/Ubuntu only!):
# Show installed kernels dpkg --list | grep linux-image # Remove ALL old kernels, keeping the latest (very careful!) # sudo apt autoremove --purge
Do not manually remove the current kernel! Theautoremovecommand usually handles this safely, but always check the list of packages before confirming.
Verify Results
After completing all steps, check how much space was freed:
df -h /
Or run ncdu / again for a visual comparison. You should see an increase in free space by 1-10 GB (depending on the system's initial state).
Potential Issues
- "Permission denied" when deleting files: Use
sudofor system directories. Ensure you are not trying to delete files owned by other users without proper permissions. - System became unstable after log deletion: This is usually temporary. Logs are recreated. If the issue persists, check if you deleted a file still in use by a process (e.g.,
syslog). Restore from backup or restart the daemon (e.g.,sudo systemctl restart rsyslog). - Not enough space to run commands: If the disk is 100% full, some commands (like
apt) may fail due to inability to create temporary files. In this case:- Manually delete the largest files found via
ncduorfind. - Clear browser or user caches in
/home. - Reboot the system—this automatically clears
/tmp.
- Manually delete the largest files found via
- Accidentally deleted a needed file: Linux has no command-line trash bin. Restore from backup or, if the file was open by a process, use
lsof | grep (filename)to find the PID and copy the file from/proc/(PID)/fd/.
Automate Cleanup
To regularly maintain disk cleanliness, set up cron jobs.
- Weekly APT cache and old package cleanup (Debian/Ubuntu):
sudo crontab -e
Add this line (runs Sundays at 3:00 AM):0 3 * * 0 apt clean && apt autoremove -y - Daily systemd journal cleanup (keep 7 days):
0 4 * * * journalctl --vacuum-time=7d - Monthly /tmp cleanup (reboot usually handles it, but for certainty):
0 2 1 * * rm -rf /tmp/*
Important: Test cron jobs manually before adding them to the schedule. Ensure they do not delete needed files.