How to Clear the Cache in Linux
Drop Linux page cache, filesystem metadata caches, or both with the right command. Learn what it clears, when to use it, and how to avoid common permission errors.
To clear selected Linux filesystem caches for a cold-cache test or debugging run, first write dirty data to storage and then ask the kernel to drop both the page cache and reclaimable filesystem metadata caches:
sudo sh -c 'sync; echo 3 > /proc/sys/vm/drop_caches'
This does not clear every cache on the machine, empty application caches, or free memory used by processes. It asks the kernel to drop clean page-cache data and reclaimable slab objects such as dentries and inodes. Linux normally reclaims these caches when other work needs memory, so forcing a drop is generally unnecessary outside testing or debugging. Linux kernel documentation and the proc_sys_vm(5) manual describe this interface and its performance cost.
Choose which cache to drop
The value written to /proc/sys/vm/drop_caches selects the cache categories:
| Value | What it requests | Use it when |
|---|---|---|
1 |
Drop page cache | Your test focuses on cached file contents. |
2 |
Drop reclaimable slab objects, including dentries and inodes | Your test focuses on filesystem metadata caches. |
3 |
Drop both categories | You need a cold-cache test covering both documented groups. |
For each value, run sync first if the goal is to make more cache objects eligible for dropping. The kernel operation does not free dirty objects; syncing writes dirty data so that objects that become clean can be dropped.
Commands for each cache category
Drop only page cache
sudo sh -c 'sync; echo 1 > /proc/sys/vm/drop_caches'
Drop reclaimable dentries and inodes
sudo sh -c 'sync; echo 2 > /proc/sys/vm/drop_caches'
Drop both categories
sudo sh -c 'sync; echo 3 > /proc/sys/vm/drop_caches'
The combined command in the introduction is a convenient form for a cold-cache test. Run it in a shell on the Linux system whose caches you intend to affect. It requires administrative privileges.
Why the sudo shell matters
Shell redirection is performed by the shell, before a command such as echo writes anything. This common form usually fails for that reason:
sudo echo 3 > /proc/sys/vm/drop_caches
Here, your unprivileged shell tries to open the procfs file for redirection. Elevating only echo does not elevate that file open. Use sudo sh -c so the shell handling the redirection runs with administrative privileges:
sudo sh -c 'sync; echo 3 > /proc/sys/vm/drop_caches'
Alternatively, an administrator can open a root shell and run the operation there:
sudo -i
sync
echo 3 > /proc/sys/vm/drop_caches
exit
What this operation does and does not clear
- Page cache: clean cached file data held by the kernel.
- Reclaimable slab objects: filesystem metadata objects such as dentries and inodes.
- Dirty objects: these are not freed by the drop operation. Run
syncfirst to write dirty data and increase the clean objects that may be eligible. - Application caches and process memory: this interface does not empty arbitrary application-managed caches or terminate processes to release their memory.
Do not describe this as a universal “clear all Linux cache” command. It targets the documented kernel cache categories only. The kernel automatically reclaims these objects when memory is needed elsewhere.
When clearing cache makes sense
Use a cache drop when a test or debugging task specifically needs to begin with clean page cache, filesystem metadata caches, or both. For example, a filesystem benchmark may need to distinguish cold-cache behavior from repeated reads served with cache. The Linux manual page notes that this interface can be used for testing or debugging.
For ordinary memory pressure, a high cache figure alone is not a reason to force a drop. The kernel uses memory for caches and can reclaim those objects as other work needs memory. Repeatedly dropping them discards useful cached data and metadata, which must be read or built again. That can increase I/O and CPU work and degrade performance.
How to run a cold-cache test carefully
- Decide whether the test needs page cache (
1), reclaimable metadata caches (2), or both (3). - Finish or pause unrelated workloads where practical, so they do not immediately repopulate the caches or distort your measurement.
- Run
syncand write the selected value to/proc/sys/vm/drop_cacheswith administrative privileges. - Start the test promptly and record the exact command, workload, and conditions so later runs can be compared.
- Expect the first reads and filesystem lookups to do more work while the kernel rebuilds caches. Compare cold-cache runs separately from warm-cache runs.
Dropping caches changes the starting condition; it does not guarantee that every subsequent operation is uncached. The workload itself can repopulate caches as it runs. The command also provides no universal benchmark result or performance improvement: its purpose is to make a selected test condition more reproducible.
Performance, reliability, and cost
The operation is documented as non-destructive: it does not free dirty objects. Its main cost is lost caching benefit. Reads and filesystem metadata lookups may require additional I/O and CPU while cached data and objects are recreated. A cache drop can therefore make normal work slower, especially if performed routinely or while important workloads are active.
Use the kernel’s automatic reclaim for normal memory management. Reserve forced drops for a specific test or debugging need, and use the narrowest value that matches that need. Check your distribution’s kernel documentation when you need version-specific behavior; the cited kernel page is the rolling latest documentation.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Permission denied when writing the procfs file |
The shell opening the file for > is not privileged, often because sudo was placed only before echo. |
Use sudo sh -c 'sync; echo 3 > /proc/sys/vm/drop_caches', or run the write from a root shell. |
/proc/sys/vm/drop_caches: No such file or directory |
The expected procfs path is unavailable in the current environment, for example because procfs is not mounted or the environment does not expose that kernel interface. | Run the command on the intended Linux host and check that /proc is mounted and the path exists. A container or restricted environment may not expose a writable host kernel interface. |
| The cache figure does not fall as expected | Dirty objects are not dropped, or active workloads have already repopulated caches. | Run sync before the drop and pause competing workloads where practical. Remember that the kernel may reclaim caches automatically, and memory reporting tools can group memory categories differently. |
| The system or benchmark gets slower afterward | The operation discarded cache contents that would have made later reads and lookups faster. | Do not repeat drops during normal use. Use them only when the test requires a cold-cache starting condition, then allow caches to rebuild. |
Or skip the browser setup
If your task is to capture a website while you work on a Linux server or benchmark, ScreenshotNeo is a website screenshot API and MCP server from ScreenshotNeo. It does not clear Linux kernel caches; it handles website capture in one GET request. See the ScreenshotNeo API documentation for request options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Cookie banners, newsletter popups, and chat widgets are removed before the shot. Bot checks, blank pages, failed loads, timeouts, and cache hits are never billed, and each response identifies its page verdict and billing status in headers. An MCP server lets AI agents such as Claude and Cursor take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots.
Sign up for ScreenshotNeo free: 1,000 screenshots a month, no card required.
FAQ
Does clearing cache delete files?
The documented operation drops selected clean kernel cache objects; it is not a command to delete the underlying files. It does not free dirty objects.
Should I clear cache whenever Linux reports high memory use?
No. The kernel can reclaim these caches when memory is needed. A high cache figure alone does not establish a memory problem.
Which value should I use for a cold filesystem test?
Use 3 when the test needs both documented categories. Use 1 or 2 when the test specifically targets only one category.
Does this restart Linux or change the setting permanently?
The command writes a request to a procfs control file; it does not configure a persistent setting or reboot the system.


