GICT knowledge base
How to Reduce Disk Usage by Cleaning Cache in Linux
Published
Disk space management is a critical task for anyone responsible for maintaining a Linux system, whether it’s a developer’s workstation, a web server, or a shared resource in an organization. Over time, caches accumulate and can consume significant disk space, leading to reduced performance, application errors, or even system outages due to full disks. By understanding what cache is, where it resides, and how to clean it safely and efficiently, you can reclaim valuable disk space and maintain optimal system performance.
This comprehensive guide explains the various types of cache found on Linux systems, step-by-step instructions for cleaning them, and best practices for setting up regular maintenance while minimizing disruption to essential services.
Prerequisites
- Access to a Linux system with terminal privileges
- (Optional) Sudo or root privileges for system-wide cache cleaning
- Basic familiarity with command-line tools and file system hierarchy
- A clear understanding of which applications and services are running on your system
What Is Cache and Why Does It Accumulate?
Cache refers to temporary data stored by the operating system, applications, or system services to accelerate access to frequently used resources. While caching improves performance, it can gradually consume large amounts of disk space if not managed properly.
Common sources of cache in Linux include:
- Package manager cache (e.g., APT, YUM): Stores downloaded packages and metadata
- Web browser cache: Temporary internet files and cookies
- System cache: Kernel and user-level temporary files
- Application cache: Data for apps like web servers, editors, or IDEs
- Thumbnail cache: Image previews for desktop environments
- Log file cache: Rotated and old log files that aren’t actively used
Step 1: Analyze Disk Usage
Before cleaning, identify where disk space is being used. The du, df, and GUI tools like baobab (Disk Usage Analyzer) help you locate large files and directories.
Check Overall Disk Usage
$ df -h
Shows disk usage for all mounted filesystems in a human-readable format.
Find Large Directories
$ sudo du -h --max-depth=1 / | sort -hr | head -n 20
Shows the 20 largest directories at the root level.
Drill Down Into Home or Var
$ sudo du -h --max-depth=1 /home
$ sudo du -h --max-depth=1 /var
This approach helps you target your cleanup efforts effectively.
Step 2: Clean Package Manager Cache
APT (Debian, Ubuntu, Mint)
APT stores package files in /var/cache/apt/archives.
- Clear the package cache:
$ sudo apt clean
Removes all cached package files.
- Remove obsolete packages:
$ sudo apt autoremove
Deletes unused dependencies.
- Remove only outdated cache:
$ sudo apt autoclean
Removes only packages that can no longer be downloaded.
YUM/DNF (CentOS, Fedora, RHEL)
YUM and DNF use /var/cache/yum or /var/cache/dnf.
- Clean all cached data:
$ sudo yum clean all
or
$ sudo dnf clean all
- List and remove orphaned packages:
$ sudo package-cleanup --leaves
and
$ sudo package-cleanup --orphans
Zypper (openSUSE/SUSE)
Clean all cache:
$ sudo zypper clean --all
Step 3: Clean System Cache and Temporary Files
/tmp and /var/tmp
Temporary files accumulate in /tmp and /var/tmp. These files are safe to delete unless in use by a running process.
Clean /tmp:
$ sudo rm -rf /tmp/*
Clean /var/tmp:
$ sudo rm -rf /var/tmp/*
Systemd Temporary Files
Systemd can manage and clean temporary files automatically.
Trigger a cleanup manually:
$ sudo systemctl start systemd-tmpfiles-clean
Configure periodic cleanup: Edit /etc/tmpfiles.d/ or /usr/lib/tmpfiles.d/ files to adjust retention times.
Step 4: Clean User Cache
The XDG cache directory stores per-user application cache, typically found at ~/.cache.
List contents and size:
$ du -h --max-depth=1 ~/.cache
Remove cache for all applications:
$ rm -rf ~/.cache/*
You can also selectively remove cache for specific applications (e.g., browsers, editors) if needed.
Step 5: Clean Browser Cache
Firefox
From the GUI: Preferences > Privacy & Security > Cookies and Site Data > Clear Data
From the terminal: Delete the cache folder:
$ rm -rf ~/.cache/mozilla/firefox/*.default-release/cache2/*
Google Chrome/Chromium
From the GUI: Settings > Privacy and security > Clear browsing data
From the terminal:
$ rm -rf ~/.cache/google-chrome/Default/Cache/* $ rm -rf ~/.cache/chromium/Default/Cache/*
Step 6: Clean Thumbnail Cache
Image viewers and file managers generate thumbnails, stored in ~/.cache/thumbnails.
Delete thumbnail cache:
$ rm -rf ~/.cache/thumbnails/*
Step 7: Clean Application-Specific Caches
1. Web Servers (e.g., Apache, Nginx)
Clear application-level or reverse proxy caches:
$ sudo rm -rf /var/cache/nginx/* $ sudo rm -rf /var/cache/apache2/*
2. IDEs and Editors
Tools like IntelliJ IDEA, VSCode, Eclipse, and others may store large caches in ~/.cache, ~/.config, or specific subdirectories. Check each application’s documentation to identify safe-to-delete cache files.
Step 8: Clean Log File Cache
Log files, especially rotated or archived logs, can consume significant disk space in /var/log.
List large log files:
$ sudo du -h /var/log | sort -hr | head -n 20
Remove old logs (with caution):
$ sudo rm -rf /var/log/*.gz /var/log/*.1 /var/log/*.old
Use logrotate to manage logs:
$ sudo logrotate -f /etc/logrotate.conf
Step 9: Clean Snap and Flatpak Caches
Snap
Snap packages create cache in /var/lib/snapd/snaps/ and do not remove old versions by default.
List installed and old snaps:
$ snap list --all
Remove old versions:
$ sudo snap remove <package> --revision=<old_revision>
Automate cleanup:
$ sudo snap set system refresh.retain=2
Flatpak
Clean Flatpak cache:
$ flatpak uninstall --unused $ rm -rf ~/.var/app/*/cache/*
Step 10: Clean Docker and Container Caches
If you use Docker or other container runtimes, images and volumes can quickly fill up your disk.
Remove unused Docker objects:
$ docker system prune
Remove dangling images and stopped containers:
$ docker image prune $ docker container prune
For Podman:
$ podman system prune
Step 11: Automate Cache Cleaning
Use Cron Jobs
Automate regular cleaning with cron jobs. For example, add this to your user’s crontab for weekly cleanup:
0 3 * * 0 rm -rf ~/.cache/* 0 4 * * 0 sudo apt clean
Use Systemd Timers
For more advanced periodic cleaning, set up a custom systemd timer targeting scripts in /usr/local/bin.
Step 12: Monitor and Prevent Excessive Cache Growth
Set up disk usage alerts: Use tools like ncdu, duf, or monitoring software (Nagios, Zabbix, Prometheus).
Configure application cache limits: Many apps allow you to set maximum cache size or configure cache expiration.
Regularly review disk usage: Incorporate disk checks into your regular maintenance routines.
Step 13: Use Dedicated Utilities
Some distributions offer utilities to simplify cache and system cleaning.
BleachBit
GUI and CLI tool for cleaning cache, logs, browser data, and more:
$ sudo apt install bleachbit $ bleachbit $ bleachbit --clean system.cache system.tmp
Stacer
GUI application for monitoring and cleaning system resources.
Safety and Best Practices
- Always back up critical data before performing large-scale deletions. Some cache files may be required for application stability or troubleshooting.
- When in doubt, consult documentation or move files to a temporary location before permanent deletion.
- Check which files are in use with lsof.
- Avoid cleaning cache during critical system or application operations.
- Automate with caution-test scripts on non-production systems first.
Conclusion
Cleaning cache is an essential part of Linux system maintenance that helps you reclaim disk space, improve performance, and reduce the risk of unexpected outages. By regularly analyzing disk usage, targeting the most significant cache contributors, and automating safe cleaning routines, you ensure your system remains responsive and reliable.
Remember, cache is there to speed up operations-clean it judiciously, not indiscriminately. Combine cache cleaning with good monitoring, logging, and backup practices for a healthy, well-maintained system.
[!NOTE] For production servers or mission-critical applications, always schedule cache cleaning during maintenance windows and notify users or stakeholders as appropriate.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
