User Agents

Web browsers and everyday web tech, explained

Clear Cache in Linux Safely: Step-by-Step Guide

· 4 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Clearing the cache in Linux safely frees up disk space, improves system responsiveness, and resolves certain application or performance issues without risking system stability. The cache includes temporary files from package managers, application caches, and memory buffers. While Linux automatically manages most cache, manually clearing it—especially package caches and user-specific temporary files—can be useful after updates, during disk space shortages, or when troubleshooting. This guide explains how to do it correctly, using safe, standard methods that preserve system integrity.

What You'll Need

  • Access to a terminal (command line)
  • sudo or root privileges for system-level cache operations
  • Basic familiarity with Linux command-line tools
  • A backup strategy for critical data (recommended before any cleanup)

Step-by-Step

  1. Open a terminal window. You can do this by pressing Ctrl+Alt+T on most desktop environments or searching for "Terminal" in your application menu.
  2. Check current disk usage to assess how much space may be recovered. Run df -h to view disk space across all mounted partitions. Identify partitions with low free space, typically / (root) or /home.
  3. Check current memory cache usage. Run free -h to see how much RAM is used by cache and buffers. This helps determine if memory cleanup is needed, though memory cache is automatically managed by the kernel.
  4. Clear the package manager cache. Most Linux distributions use apt (Debian/Ubuntu) or dnf (Fedora/RHEL). For apt, run sudo apt clean to remove downloaded package files. For dnf, use sudo dnf clean all. This removes cached .deb or .rpm files not needed after installation.
  5. Remove unused packages and dependencies. Run sudo apt autoremove (Debian/Ubuntu) or sudo dnf autoremove (Fedora) to delete orphaned dependencies no longer required by installed software.
  6. Clear user application caches. Navigate to your home directory and remove contents of ~/.cache: rm -rf ~/.cache/*. This deletes temporary files from browsers, desktop apps, and tools. Avoid deleting the entire .cache folder—just its contents.
  7. Optionally clear thumbnail cache. Run rm -rf ~/.cache/thumbnails/* to delete cached image thumbnails, which regenerate as needed.
  8. Clear system memory cache (only if necessary). This requires root access and should be done with caution. First, sync pending writes: sync. Then, to clear pagecache, dentries, and inodes, run echo 3 | sudo tee /proc/sys/vm/drop_caches. This forces the kernel to drop clean cache—do not do this on production servers under load.
  9. Verify cleanup success. Re-run df -h and free -h to confirm increased free disk space and reduced cache usage.
  10. Reboot (optional). While not always needed, a reboot ensures all services restart cleanly and caches rebuild properly, especially after large cleanups.

Common Mistakes

One frequent error is using rm -rf / or similar destructive commands, often due to typos. Always double-check paths before running deletion commands. Another mistake is clearing /tmp manually—some systems rely on automatic cleanup, and deleting active temporary files can crash running applications. Avoid deleting ~/.cache itself—only its contents. Users sometimes run drop_caches unnecessarily, which can degrade performance temporarily by forcing the system to re-read data from disk. Lastly, skipping sync before dropping memory caches risks data loss from unwritten buffers.

Tips

To make cache cleanup routine, create a shell script that runs safe commands like apt clean and rm -rf ~/.cache/*, but avoid including dangerous operations like drop_caches unless specifically needed. Schedule it with cron monthly, but test manually first. Use du -sh ~/.cache to monitor cache size over time. For GUI users, tools like bleachbit (install via sudo apt install bleachbit) offer visual cache cleanup, but run with caution and review selections. Always check which files a tool will delete before confirming.

Troubleshooting

If you encounter a "permission denied" error, ensure you're using sudo for system-level operations. If apt clean fails, check your internet connection or run sudo apt update to refresh the package list. After clearing ~/.cache, some applications may take longer to start as they rebuild caches—this is normal. If the system behaves oddly post-cleanup, check logs with journalctl -b for errors. If drop_caches causes slowdowns, remember it’s temporary—the kernel will rebuild cache as data is accessed. Never run drop_caches on a production server during peak usage. If you accidentally delete critical files, restore from backup or use package manager to reinstall affected software.

Frequently Asked Questions

Does clearing cache delete personal files?

No, cache includes temporary files only. Personal documents, photos, and settings are not stored in cache directories.

Is it safe to clear memory cache with drop_caches?

Yes, if done correctly. Always run sync first. However, it’s rarely needed on desktop systems and can temporarily slow performance.

How often should I clear cache?

For package caches, monthly or after major updates. User app caches can be cleared quarterly or when disk space is low.

Can clearing cache break my system?

Not if you follow safe methods. Avoid deleting system directories or using wildcards in wrong locations. Stick to apt clean, dnf clean, and ~/.cache/*.

What’s the difference between cache and logs?

Cache stores temporary data for faster access; logs record system events. Clearing logs (/var/log) is separate and should be done via log rotation tools, not manual deletion.

Does Linux automatically clear cache?

Yes, the kernel automatically frees memory cache when needed. Disk-based package and app caches are not auto-deleted, so manual cleanup helps reclaim space.