Reduce Linux Disk Usage by Cleaning Cache Files
In this article
Reducing disk usage by cleaning the cache in Linux involves removing temporary files stored by the package manager, system logs, and user applications to reclaim storage space. This process removes redundant data that the system keeps to speed up future operations but which can accumulate over time and consume significant gigabytes of disk space. Performing these steps helps maintain system performance and prevents "disk full" errors that can crash applications or prevent the system from booting.
What You'll Need
- A computer running a Linux distribution (e.g., Ubuntu, Debian, Fedora, CentOS).
- Access to the terminal (command line).
- Administrative privileges (sudo access) to modify system-level directories.
Step-by-Step
-
Check Current Disk Usage: Before cleaning, identify how much space is being used. Open your terminal and run
df -h. This command displays the amount of disk space used and available on all mounted filesystems in a human-readable format. -
Clean the APT Package Cache (Debian/Ubuntu): The package manager stores every
.debfile it downloads in/var/cache/apt/archives/.- Run
sudo apt-get cleanto remove all cached package files. This is safe as the system will simply download them again if you need to reinstall a package. - Run
sudo apt-get autocleanto remove only the cached packages that can no longer be downloaded (obsolete versions).
- Run
-
Clean the DNF/YUM Cache (Fedora/CentOS/RHEL): Similar to APT, Red Hat-based systems cache metadata and packages.
- Run
sudo dnf clean all(orsudo yum clean allon older versions) to remove cached package data and metadata.
- Run
-
Clear Systemd Journal Logs: The system journal can grow indefinitely if not capped.
- Check the size of the logs using
journalctl --disk-usage. - Clear logs older than a specific time, for example, 3 days:
sudo journalctl --vacuum-time=3d. - Alternatively, limit logs to a specific size:
sudo journalctl --vacuum-size=100M.
- Check the size of the logs using
-
Empty the User Cache Directory: Each user has a local cache folder located at
~/.cache. This contains data from web browsers, thumbnails, and application states.- Navigate to the directory:
cd ~/.cache. - List the largest folders:
du -sh * | sort -h. - Delete specific large cache folders you no longer need:
rm -rf folder_name. Be cautious not to delete the entire directory if you want to keep application settings.
- Navigate to the directory:
-
Clear Thumbnail Cache: Linux creates thumbnails for images and videos which are stored in
~/.cache/thumbnails.- Run
rm -rf ~/.cache/thumbnails/*to clear these. The system will regenerate them as needed when you browse folders.
- Run
-
Clear PageCache, Dentries, and Inodes (RAM Cache): While this doesn't free up disk space, it frees up system memory. This should only be done if necessary.
- Run
sudo sync; echo 3 | sudo tee /proc/sys/vm/drop_caches.
- Run
Common Mistakes
- Using
rm -rf /or incorrect paths: A typo in a delete command can wipe critical system files. Always double-check the path before pressing Enter. - Deleting configuration files instead of cache: Ensure you are deleting files in
.cacheand not in.config. Deleting.configwill reset your application settings. - Over-cleaning: Clearing caches too frequently can actually slow down your system, as the computer must re-download packages or re-generate thumbnails and metadata.
- Running commands without sudo: System-level caches (like APT or Journald) require root permissions; running them without
sudowill result in "Permission Denied" errors.
Tips
- Use NCDU: Install
ncdu(NCurses Disk Usage) using your package manager. It provides an interactive visual interface to find exactly which directories are consuming the most space. - Automate with Cron: If your logs grow quickly, set up a cron job to run
apt-get autocleanorjournalctl --vacuum-timeonce a month. - Limit Journal Size Permanently: Edit
/etc/systemd/journald.confand setSystemMaxUse=200Mto prevent logs from growing too large in the first place. - Check /tmp: Occasionally, the
/tmpdirectory contains large temporary files. While these are usually cleared on reboot, you can manually check them if disk space is critical.
Troubleshooting
If you have cleared the caches but df -h still shows the disk is full, consider the following:
- Deleted files still held by processes: If you delete a large log file while a process is still writing to it, the space is not reclaimed. Use
lsof +L1to find deleted files that are still open and restart the associated service. - Hidden files: Ensure you are checking hidden directories (those starting with a dot) in your home folder using
ls -la. - Inode Exhaustion: Sometimes you have disk space, but you have too many small files, exhausting the "inodes". Check this with
df -i. If inodes are at 100%, you must delete a large number of small files rather than a few large ones.
Frequently Asked Questions
Is it safe to run apt-get clean?
Yes. It only removes the downloaded .deb installers. It does not uninstall any software or remove configuration files.
Will clearing the cache slow down my computer?
Temporarily, yes. The first time you open an app or browse a folder after clearing the cache, it may take slightly longer to load as the system regenerates the necessary temporary data.
What is the difference between clean and autoclean?
clean removes every package from the cache, while autoclean only removes packages that are no longer available in the repositories.
How often should I clean my Linux cache?
For most users, once every few months is sufficient. If you have a very small SSD, you may want to do it monthly.
Can I clear the cache using a GUI?
Yes, tools like BleachBit provide a graphical interface to perform these tasks safely without using the terminal.