Complete Linux Cleanup: Safely Freeing Up Disk Space
Over time, any Linux system—especially on servers or older laptops with SSDs—starts to run out of disk space. This can cause update errors, service failures, and system slowdowns. In this guide, you'll learn how to safely and effectively clean your system of temporary files, package caches, and unnecessary data without touching user documents or configurations.
Why Is This Important?
A full root partition (/) is a common cause of failures. Many services (like MySQL, Docker, web servers) require free space to operate. Regular cleanup is part of basic system maintenance. We'll focus on safe methods that won't delete your personal files.
Step 1: Analyze Disk Usage
Before deleting anything, find out what's actually taking up space.
1.1 Check Free Space on Partitions
df -h
The -h flag outputs sizes in a human-readable format (MB, GB). Pay attention to the Use% column. It's critical if it's above 85-90%.
1.2 Find Large Directories
sudo du -sh /* 2>/dev/null | sort -rh | head -20
This command shows the sizes of top-level directories in root (/), sorts them in descending order, and outputs the top 20. Usually, the largest are /usr, /var, /home.
💡 Tip: For a more detailed analysis, install
ncdu(sudo apt install ncdu). It's an interactive tool that lets you "browse" the filesystem and see directory sizes. Runsudo ncdu /.
Step 2: Clean Package Manager Cache
All distributions store downloaded package files (.deb, .rpm) for potential reinstallation. These can be safely removed.
2.1 For Debian/Ubuntu (APT)
# Clean the entire cache (all downloaded .deb files)
sudo apt clean
# A gentler option: remove only obsolete files
sudo apt autoclean
After clean, the next apt upgrade will need to download packages again.
2.2 For Fedora/RHEL/CentOS (DNF)
sudo dnf clean all
Removes all DNF caches, including repository metadata.
2.3 For Arch Linux (Pacman)
# Clean package cache (old versions are removed, current ones stay)
sudo paccache -r
# VERY aggressive cleanup: removes ALL cached packages
# sudo pacman -Scc # WARNING! Use with caution, only if you're sure
Step 3: Remove Unnecessary Dependencies and Old Kernels
During system updates, old versions of libraries and Linux kernels often remain. These can take up hundreds of megabytes.
3.1 For Debian/Ubuntu
# Removes packages that were installed as dependencies
# but are no longer needed by any installed package.
# --purge also removes the configuration files of these packages.
sudo apt autoremove --purge
Important: The command will propose packages to remove. Check the list carefully. If you manually installed a package via apt install, it won't be removed by autoremove.
3.2 For Fedora/RHEL
sudo dnf autoremove
3.3 Manually Removing Old Kernels (if autoremove didn't handle it)
# Show installed kernels
dpkg -l 'linux-image*' | grep ^ii # Debian/Ubuntu
rpm -qa | grep kernel # Fedora/RHEL
# Remove a specific old kernel (example)
sudo apt remove linux-image-5.4.0-42-generic
Keep at least one working kernel (current and previous for rollback).
Step 4: Clean Temporary Files and Logs
4.1 Temporary Files in /tmp
# Delete files older than 10 days in /tmp (safe)
sudo find /tmp -type f -atime +10 -delete
# Delete empty directories in /tmp
sudo find /tmp -type d -empty -delete
⚠️ Important: Don't aggressively delete everything in
/tmp(rm -rf /tmp/*). Some applications may be using that directory right now. It's better to delete old files.
4.2 Logs in /var/log
Service logs can grow large. Clean them if you're sure they're not needed for debugging.
# Clean old logs (e.g., older than 7 days)
sudo find /var/log -type f -name "*.log" -mtime +7 -delete
# Clean logs for a specific service (e.g., nginx)
sudo truncate -s 0 /var/log/nginx/access.log
sudo truncate -s 0 /var/log/nginx/error.log
Alternative: Use logrotate—it automatically rotates and compresses logs. Check its configuration in /etc/logrotate.conf.
4.3 systemd Journal Logs
# Keep logs only for the last 3 days (or specify size: --vacuum-size=100M)
sudo journalctl --vacuum-time=3d
# Show current journal size
sudo journalctl --disk-usage
Step 5: Find and Remove Large Unnecessary Files
Sometimes space is taken up by non-system files: downloads, ISO images, browser caches.
5.1 Find Files Larger Than 100 MB
sudo find / -type f -size +100M 2>/dev/null | head -20
2>/dev/null hides access errors (e.g., to /proc). Manually check the found paths before deleting.
5.2 Clean Browser Caches (example for Firefox)
# Firefox cache for current user
rm -rf ~/.cache/mozilla/firefox/*.default/cache2/
Replace *.default with your profile name (check in ~/.mozilla/firefox/).
5.3 Delete Old Downloads
# Show files in ~/Downloads older than 30 days
find ~/Downloads -type f -mtime +30 -ls
# Delete them (check the list first!)
find ~/Downloads -type f -mtime +30 -delete
Step 6: Check the Result
After all cleanups, run again:
df -h
You should see increased free space in the / partition (usually by 1-5 GB, depending on the system).
Automation (Optional)
To avoid forgetting cleanup, set up regular tasks.
For APT-based Systems (Debian/Ubuntu)
Create a file /etc/apt/apt.conf.d/02autoremove with:
DPkg::Post-Invoke {"if [ -x /usr/bin/apt-autoremove ]; then apt-autoremove -y; fi";};
This automatically runs autoremove after every apt upgrade. Use with caution on production servers.
For systemd Journal
In /etc/systemd/journald.conf, set:
SystemMaxUse=100M
This limits the journal size to 100 MB.
Final Recommendations
- Never manually delete files in
/usr,/bin,/sbin,/lib. This will break the system. - Back up important data before mass operations.
- Regularity is key. Cleaning once a month prevents problems.
- To monitor free space, add an alias to
~/.bashrc:
Nowalias dfh='df -h | grep -E "^(/dev|Filesystem)"'dfhquickly shows disk status.
Following this guide helps keep your system clean and avoids critical disk space shortages.