Clear Linux Cache using bash script
How to Clear System Caches on Linux with a Simple Script
Linux systems are known for their reliability and efficiency, but over time, caches, logs, and temporary files can accumulate, consuming valuable resources. Whether you're trying to free up space or optimise system performance, managing these caches is essential.
In this article, you'll learn how to use a Bash script to clear various caches on your Linux system, including APT, memory, swap, DNS, and more. Let’s dive into how this script works and why it’s so effective.
---
The Problem with Unmanaged Caches
Caches are temporary storage areas that hold data to speed up processes. While helpful, they can also:
Occupy significant disk space.
Slow down systems when RAM is overloaded.
Cause issues with outdated data, such as DNS entries.
The solution? A comprehensive cache-clearing script that ensures your system stays optimised.
---
Introducing the Cache-Clearing Script
Here’s a Bash script that clears various caches on a Linux system:
#!/bin/bash
# Clear APT cache
echo "Clearing APT cache..."
sudo apt-get clean
sudo apt-get autoclean
# Clear System Memory Cache (RAM)
echo "Clearing system memory cache..."
sudo sync
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
# Clear Swap Memory
echo "Clearing swap memory..."
sudo swapoff -a
sudo swapon -a
# Clear DNS Cache
echo "Clearing DNS cache..."
sudo systemctl restart systemd-resolved
# Clear Thumbnail Cache
echo "Clearing thumbnail cache..."
rm -rf ~/.cache/thumbnails/*
# Clear Logs (Optional)
echo "Clearing old logs..."
sudo journalctl --vacuum-time=2d
echo "All cache cleared!"
---
Step-by-Step Explanation of the Script
1. Clearing APT Cache
sudo apt-get clean
sudo apt-get autoclean
Purpose: Removes cached package files and obsolete packages, freeing disk space.
---
2. Clearing System Memory Cache (RAM)
sudo sync
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
Purpose: Frees up memory by clearing:
Page Cache (frequently accessed files).
Dentries (directory entries).
Inodes (file system metadata).
---
3. Clearing Swap Memory
sudo swapoff -a
sudo swapon -a
Purpose: Turns swap off and back on, refreshing swap memory and reducing its usage.
---
4. Clearing DNS Cache
sudo systemctl restart systemd-resolved
Purpose: Resets the DNS resolver, ensuring outdated or invalid DNS entries are removed.
---
5. Clearing Thumbnail Cache
rm -rf ~/.cache/thumbnails/*
Purpose: Deletes cached image thumbnails, which can accumulate and occupy disk space.
---
6. Clearing Logs (Optional)
sudo journalctl --vacuum-time=2d
Purpose: Removes journal logs older than 2 days to free up disk space.
---
How to Use the Script
1. Save the Script: Copy the script into a file named clear_all_caches.sh.
2. Make it Executable: Run:
chmod +x clear_all_caches.sh
3. Execute the Script: Use sudo to run the script:
sudo ./clear_all_caches.sh
---
When Should You Use This Script?
1. Free Up Disk Space: After large installations or updates.
2. Optimise Performance: When your system feels sluggish.
3. Refresh Data: If you're encountering DNS or thumbnail issues.
---
Final Thoughts
Managing caches is an integral part of maintaining a fast and efficient Linux system. This script 9 the process, saving time and effort while ensuring your system runs smoothly.
Ready to optimise your Linux machine? Save the script, make it executable, and start clearing those caches today!
---
Feel free to visit my github for the scripts:
Debian: https://github.com/enricagra/linux-cache-clear/blob/main/clear-cache.sh
RHEL: https://github.com/enricagra/linux-cache-clear/blob/main/cache-clear-rhel
Comments
Post a Comment