Linux automatically uses available memory to cache files and speed up system performance. This behaviour is both normal and efficient. However, during heavy workloads, performance testing, or troubleshooting, you may need to manually clear cached memory to free resources or reset memory usage.
This guide provides clear, reliable instructions to safely clear the cache on Linux systems.
Table of Contents
Why Clearing Cache Matters?
Clearing the cache is helpful when:
- Your server is running low on free memory
- Applications are not releasing memory properly
- You need accurate RAM usage for testing
- You want to benchmark or troubleshoot performance
How Linux Caching Works?
Linux is intended to make your system faster by using unused RAM to store recently accessed data. Instead of letting memory sit idle, Linux turns it into a high-speed cache that helps applications load files more quickly.
Linux mainly uses three types of cache:
- Page Cache
Stores actual file contents in memory. When a file is opened again, Linux can load it directly from RAM instead of reading it from the slower disk. - Dentries and Inodes
These store directory paths, file names, and information about files (such as permissions and owners). This speeds up operations like navigating folders or checking file details.
In normal operation, Linux automatically manages this cached memory and frees it when applications need more space. However, administrators may clear the cache manually during troubleshooting, testing, or memory debugging.
Clearing the cache is safe, but after clearing, the system may run slightly slower until it rebuilds the cache naturally. Root or sudo access is required to perform these actions.
Check Current Memory Usage
Run:
free -h
This helps you compare memory usage before and after clearing the cache.
Safely Clear Cache (Step-by-Step)
- Sync Disk Data
Before clearing the cache, it is important to ensure that all pending data is written to the disk. This prevents accidental data loss.
Run:sync
This command doesn’t clear anything; it simply saves all buffered data.
- Clear the Cache
Linux provides three safe options to clear the memory cache. Use the one that fits your needs.- Clear Only Page Cache
echo 1 > /proc/sys/vm/drop_caches
Use this if you only want to remove cached file data.
- Clear Only Dentries and Inodes
echo 2 > /proc/sys/vm/drop_caches
This removes cached directory and file metadata.
- Clear Page Cache, Dentries & Inodes (Most Common)
echo 3 > /proc/sys/vm/drop_caches
This clears everything: file data + directory & file metadata.
It is the most commonly used option for troubleshooting.
- Clear Only Page Cache
- Recommended One-Line Command
Most system admins use this:sync && echo 3 > /proc/sys/vm/drop_caches
It ensures data safety (sync) and clears all cache types in one go.
Optional: Automate Cache Clearing via Cron
If you want the system to clear the cache automatically at a fixed time, create a cron job.
Example: Clear the cache every night at midnight
0 0 * * * root sync && echo 3 > /proc/sys/vm/drop_caches
Run the following command:
crontab -e
Then paste the cron entry into the file and save.
Verify Memory After Clearing Cache
free -h
You should notice a reduction in cached memory.
Best Practices
- Only clear the cache when needed.
- Linux manages memory efficiently; avoid frequent cache clearing.
- Monitor system performance before and after clearing.
- Avoid scheduling cache clearing on production systems unless required.
Our Linux VPS Hosting plans offer dedicated memory, root access and the flexibility to scale as your requirements grow.
Conclusion
This way, clearing the cache in Linux can be extremely helpful for diagnosing performance issues, optimising memory usage, and testing how applications behave under fresh system conditions. The methods shared in this guide follow safe, reliable, and widely accepted system administration practices.
If you need any assistance, our support team is always available to help with performance tuning, memory-related concerns, or overall system optimisation.
Looking to clear cache at the server level? Check out our guide on How to clear the Nginx cache from WHM