Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud
Browsers and OS

How to Clear Cache in Linux

Learn how to clear cache in Linux, including RAM, buffer, and swap space, to boost system performance and keep your system responsive with simple commands.

Author

Tahneet Kanwal

January 11, 2026

In Linux, cache memory, swap space, and buffers are used to speed up data access and enhance system performance. However, as these areas accumulate outdated or unnecessary data, they can start to slow down the system.

Much like clearing the cache in a browser, knowing how to clear cache in Linux is crucial for maintaining optimal performance. Regularly clearing these caches ensures that your system remains responsive and efficient.

Steps to Clear RAM Memory Cache in Linux

Clearing RAM memory cache on Linux can help improve system performance by freeing up memory used by cached data. Most Linux systems provide three different ways to clear the cache. These methods can be used without affecting running processes or services.

Most Linux systems provide three ways to clear the cache. These methods can be used without affecting running processes or services.

Below are the three different ways:

Clear PageCache Only

The PageCache stores recently used data and files. Clearing it doesn’t affect running processes and is the safest method. To clear only the PageCache, use the following command:

Command:

sync; echo 1 > /proc/sys/vm/drop_caches

Steps:

  • Open the terminal.
  • To check memory usage before clearing the cache, type:
  • free
    

    This command shows memory usage, including swap and available memory. Run this before and after clearing the cache to see changes.

  • To clear the PageCache, type:
  • sync; echo 1 > /proc/sys/vm/drop_caches

This will clean the PageCache and free up memory used by cached file data.

Clear Dentries and Inodes

Dentries and inodes are used by the system to manage files and directories. Clearing these entries can help when the system’s filesystem holds a large number of files in memory. To clear only dentries and inodes, use:

Command:

sync; echo 2 > /proc/sys/vm/drop_caches

Steps:

  • Open the terminal.
  • Type the following command to check memory usage before clearing the cache:
  • free

To clear dentries and inodes, type:

sync; echo 2 > /proc/sys/vm/drop_caches

This will remove the cached directory and file metadata from memory.

Clear Dentries, Inodes, and PageCache

This method clears the PageCache, dentries, and inodes, freeing up the most memory. However, it’s not recommended for production environments or frequent use as it can impact system performance.

Command:

sync; echo 3 > /proc/sys/vm/drop_caches

Steps:

  • Open the terminal.
  • Type the following command to check memory usage:
  • free
  • To clear all caches, type:
  • sync; echo 3 > /proc/sys/vm/drop_caches

This will clear the PageCache, dentries, and inodes, freeing up memory.

Steps to Clear Swap Space Cache in Linux

Clearing the swap space in Linux involves turning off the swap, which moves the data from the swap partition back into RAM. This process can help free up memory and potentially improve system performance, especially when your system has been using swap space heavily.

  • Turn Off Swap: To clear the swap space, disable the swap partition using the swapoff. This will transfer data from the swap back into RAM.

Command:

Steps:

    • Open the terminal by pressing Ctrl + Alt + T.
    • Run the following command to disable swap:
    • sudo swapoff -a 
    • To check memory usage, run:
  • Turn Swap Back On: After clearing the swap, you can re-enable it by using the swapon command. This will allow the system to start using swap space again.

Command:

    sudo swapon -a
    

Steps:

    • To re-enable the swap, type the following command:
    • Verify that the swap is back on by running the following:

Adjust Swappiness

Swappiness is a kernel parameter that controls how aggressively the system uses swap space. By default, it is set to 60, meaning the system will begin swapping when memory usage exceeds 60%. Adjusting this value can help you optimize memory usage based on your system’s needs.

Here are the steps to adjust swappiness:

1. View Current Swappiness Value: To check the current swappiness value, run the following command:

cat /proc/sys/vm/swappiness

This will display the current swappiness value, which is typically set to 60 by default.

2. Change Swappiness Value: To change the swappiness value, you can use the sysctl command. The swappiness value can be set anywhere between 0 and 100:

  • 0: The system will avoid using swap as much as possible.
  • 100: The system will prefer swapping over freeing up pages in memory.
sudo sysctl vm.swappiness=x

Replace x with a value between 0 and 100. For example, to set swappiness to 20:

sudo sysctl vm.swappiness=20

This command will change the swappiness value for the current session.

3. Verify the Change: To ensure the swappiness value has been successfully changed, run:

This will display the new swappiness value.

Steps to Automatically Clear Cache in Linux

To automate the process of clearing memory, you can set up a cron job to run the commands at regular intervals. This way, the cache and swap space will be cleared automatically without doing it manually.

Follow these steps:

  • Run the command given in the terminal below.
  • To clear the cache at a specific time, add the following line to the crontab file:
  • To clear the swap space at the same time, add this line below the previous one:
  • If using nano as the editor, press CTRL + X, then press Y to confirm saving the changes, and press Enter to finalize.
  • If using vim, press ESC, type :wq, and press Enter to save and exit.
  • To verify that the cron jobs have been successfully added, run the following command:

Steps to Clear Buffer Cache in Linux

Clearing the buffer cache in Linux can help free up memory resources used by the system for caching files and directory metadata. To clear the buffer cache in Linux, follow these steps:

  • Open the terminal on your Linux machine.
  • Sync the file system to ensure all data in memory is written to disk:
  • Clear the buffer cache by running this command:

Conclusion

Clearing cache for RAM, buffer, and swap space in Linux can significantly enhance system performance by freeing up resources. While it’s safe to clear these areas when needed, it may cause a brief slowdown as the system rebuilds the cache.

To streamline the process, automating cache management with cron jobs ensures your system stays efficient without manual intervention. Regularly clearing the cache in Linux helps maintain smooth performance and ensures that your system remains responsive.

sudo swapoff -a
free -m

Adjust Swappiness

Steps to Automatically Clear Cache in Linux

crontab -e
30 3 * * * sudo sync; echo 3 > /proc/sys/vm/drop_caches
30 3 * * * sudo swapoff -a && sudo swapon -a
crontab -l

Steps to Clear Buffer Cache in Linux

sudo sync
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'

Conclusion

Author

Tahneet Kanwal is a freelance technical content writer with over 2 years of hands-on experience in frontend development and technical writing. She holds a B.Tech in Information Technology from University College of Engineering and Technology (UCET). Tahneet creates clear, SEO-optimized content on web technologies, software testing, and automation tools, leveraging her skills in HTML, CSS, JavaScript, React, Tailwind CSS, and various tools like VS Code, GitHub, Figma, and Canva. She is the author of 30+ technical blogs and an open-source contributor through Hacktoberfest. She has also participated in the Google Cloud Arcade Facilitator Program and holds certifications as a Meta Android Developer (Coursera) and in Web Development (Internshala). Over time, she has evolved her writing to prioritize structure, readability, and SEO while maintaining technical depth.

Close

Summarize with AI

ChatGPT IconPerplexity IconClaude AI IconGrok IconGoogle AI Icon

Frequently asked questions

Did you find this page helpful?

More Related Hubs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests