System performance in Linux can suffer when cached data accumulates, making cache clearing a regular maintenance task. If you’re wondering how to clear cache in linux, you’re in the right place. This guide covers everything from memory caches to DNS and package manager caches, all with simple commands you can run right now.
Cached data helps speed up your system, but sometimes it builds up and slows things down. Clearing it can free up RAM and disk space, especially after heavy use or updates. Let’s walk through the steps safely and effectively.
Understanding Linux Cache Types
Linux uses several types of cache to improve performance. Knowing which one you’re dealing with helps you choose the right clearing method.
Page Cache
This stores recently accessed file data in RAM. It’s the most common cache and speeds up file reads. Clearing it frees memory but may slow down subsequent file access temporarily.
Dentries And Inodes Cache
These store filesystem metadata like directory entries and file attributes. They help the system quickly locate files without reading the disk every time.
Slab Cache
This is used for kernel data structures. It’s more specialized and rarely needs manual clearing unless you’re debugging memory issues.
DNS Cache
Your system may cache DNS lookups to speed up domain name resolution. This cache can become stale if you change network configurations.
Package Manager Cache
Package managers like apt, yum, and dnf store downloaded packages. Over time, these can take up gigabytes of disk space.
How To Clear Cache In Linux
Now let’s get into the actual commands. The exact method depends on what type of cache you want to clear. Always be careful—clearing the wrong cache can cause temporary slowdowns or data loss if you’re not careful.
Clearing Memory Cache (Page Cache, Dentries, Inodes)
The easiest way to clear memory caches is using the /proc/sys/vm/drop_caches interface. You need root privileges for this.
- Open a terminal.
- Run
sudo syncto flush filesystem buffers. This writes any pending data to disk. - Then run
echo 1 | sudo tee /proc/sys/vm/drop_cachesto clear page cache only. - For dentries and inodes, use
echo 2 | sudo tee /proc/sys/vm/drop_caches. - To clear all three, use
echo 3 | sudo tee /proc/sys/vm/drop_caches.
After running these commands, your free memory should increase. Note that this is a non-destructive operation—it just frees up unused cached data. Your system will rebuild the cache as needed.
Important Safety Note
Never run echo 3 on a production server unless you know what you’re doing. It can cause a temporary performance hit as the system re-caches data. For desktop use, it’s generally safe.
Clearing Swap Memory
If you have swap enabled, you might want to clear it too. This moves swapped-out data back into RAM. Run sudo swapoff -a && sudo swapon -a. This requires free RAM to hold the swapped data, so check memory first with free -h.
Clearing DNS Cache
Different systems use different DNS caching services. Here are the common ones:
- systemd-resolved: Run
sudo systemd-resolve --flush-cachesorsudo resolvectl flush-caches. - dnsmasq: Run
sudo systemctl restart dnsmasq. - nscd: Run
sudo /etc/init.d/nscd restartorsudo systemctl restart nscd. - BIND: Run
sudo rndc flush.
If you’re not sure which service you use, check with ps aux | grep -E "dnsmasq|systemd-resolved|nscd|named".
Clearing Package Manager Cache
This is one of the biggest space savers. Package managers store downloaded .deb or .rpm files after installation.
For APT (Debian, Ubuntu, Linux Mint)
sudo apt cleanremoves all package files from/var/cache/apt/archives/.sudo apt autocleanremoves only outdated package files.sudo apt autoremoveremoves unused dependencies.
For YUM (CentOS 7, RHEL 7)
sudo yum clean allclears metadata and package cache.sudo yum autoremoveremoves orphaned packages.
For DNF (Fedora, RHEL 8+)
sudo dnf clean alldoes the same as yum.sudo dnf autoremovecleans up unused packages.
For Pacman (Arch Linux)
sudo pacman -Scremoves old packages from cache.sudo pacman -Sccremoves all packages from cache.- You can also use
paccache -rto keep only the last few versions.
Clearing Thumbnail Cache
Desktop environments like GNOME and KDE store thumbnails of images and videos. These can accumulate over time.
For GNOME, run rm -rf ~/.cache/thumbnails/*. For KDE, run rm -rf ~/.cache/thumbnails/* as well—they use the same location. You can also clear ~/.thumbnails/ if it exists.
Clearing Browser Cache
Browsers store a lot of data. While you can clear it from the browser settings, you can also delete the cache directories manually.
- Firefox:
rm -rf ~/.cache/mozilla/firefox/*.default*/cache/* - Chrome/Chromium:
rm -rf ~/.cache/google-chrome/*or~/.cache/chromium/* - Brave:
rm -rf ~/.cache/Brave-Browser/*
Be careful—this logs you out of websites and removes stored data. It’s often better to use the browser’s built-in clear history feature.
Clearing Systemd Journal Logs
Systemd journals store system logs. They can grow large over time. To clear them, use sudo journalctl --vacuum-time=2d to keep only the last two days, or sudo journalctl --vacuum-size=100M to limit to 100 MB.
You can also set limits in /etc/systemd/journald.conf by uncommenting and adjusting SystemMaxUse=.
Clearing Temporary Files
The /tmp directory is cleared on reboot by default, but /var/tmp persists. Use sudo rm -rf /tmp/* with caution—only delete files you know are safe. Better yet, use sudo tmpreaper or sudo tmpwatch if installed.
Automating Cache Clearing
You don’t have to do this manually every time. Create a simple script or use cron jobs.
Simple Bash Script
#!/bin/bash
# Clear memory cache
sudo sync
echo 3 | sudo tee /proc/sys/vm/drop_caches
# Clear apt cache
sudo apt clean
# Clear journal logs older than 3 days
sudo journalctl --vacuum-time=3d
Save it as clearcache.sh, make it executable with chmod +x clearcache.sh, and run it with sudo ./clearcache.sh.
Using Cron
To run it weekly, edit your crontab with sudo crontab -e and add:
0 3 * * 0 /path/to/clearcache.sh
This runs every Sunday at 3 AM. Adjust the path as needed.
Monitoring Cache Usage
Before and after clearing, check your memory usage with free -h or cat /proc/meminfo. For disk cache, use df -h to see free space on partitions.
Tools like htop or glances give real-time views. For package cache sizes, check du -sh /var/cache/apt/archives or du -sh /var/cache/dnf.
Common Mistakes And Precautions
- Don’t clear cache while important processes are running—it can cause temporary lag.
- Avoid using
echo 3on servers with heavy I/O loads. - Always run
syncfirst to avoid data loss. - Double-check commands before running them as root.
- Some cache clearing is unnecessary—Linux manages memory well on its own.
When To Clear Cache
You don’t need to do this daily. Consider clearing cache when:
- Your system feels sluggish after updates or heavy file operations.
- You’re running out of disk space.
- You’re troubleshooting memory issues.
- You’ve changed network settings and DNS is stale.
- You want to free up RAM for a specific task.
Frequently Asked Questions
Is It Safe To Clear Cache In Linux?
Yes, for most caches. Memory cache clearing is non-destructive. Package cache clearing is safe because you can re-download packages. Just avoid deleting critical system files.
How Do I Clear Cache In Linux Without Root?
You can clear user-level caches like browser cache, thumbnail cache, and ~/.cache directories without root. System caches require sudo.
Will Clearing Cache Improve Performance?
It can free up RAM and disk space, which may help if you’re low on resources. But Linux is designed to use cache efficiently, so clearing it often doesn’t give a noticeable boost.
What Is The Command To Clear Cache In Linux?
The most common command is sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches for memory cache. For package cache, use sudo apt clean or sudo yum clean all.
How Often Should I Clear Cache In Linux?
Only when needed. For most users, once a month or after major updates is enough. Servers may benefit from automated weekly cleaning of logs and package cache.
Final Thoughts
Knowing how to clear cache in linux is a useful skill for any user. It helps you reclaim resources and troubleshoot performance issues. Start with the simple commands above, and always verify what you’re deleting. With practice, you’ll know exactly when and how to clear each type of cache.
Remember that Linux is efficient at managing its own cache. Over-clearing can actually hurt performance because the system has to rebuild the cache from scratch. Use these techniques judiciously, and your system will stay snappy and responsive.
If you found this guide helpfull, share it with other Linux users. And if you have questions about specific cache types, drop a comment below. Happy clearing!