The .cache folder(The folder with cache) is eating up my space most of the time and I always how to clear it. How do I get Ubuntu to automatically delete every cache file, including clearing the recycle bin whenever it deletes it but only files that were in the cache folder not the ones deleted by me?


WARNING: The .cache folder can contain data including browser history and preferences. Please see Is it okay to delete ~/.cache? for more information.

If you would still like to continue, the best way is to have a cron job that regularly empties the recycle bin and deletes .cache. Your computer may act weirdly until a reboot, so it is probably best to do this at boot time.

Create a script named clearcache.sh in /usr/local/bin. You may need root permissions. Replace yourusername with your username. Edit it with your favourite text editor and add the following content:

rm -rf /home/yourusername/.cache /home/yourusername/.local/share/Trash
mkdir /home/yourusername/.cache /home/yourusername/.local/share/Trash

Run crontab -e and add the following line at the end of the file to run every 30 minutes:

*/30 * * * * /bin/bash /usr/local/bin/clearcache.sh >/dev/null 2>&1

Or this line for every boot:

@reboot /bin/bash /usr/local/bin/clearcache.sh >/dev/null 2>&1

Or you can use a different time in the crontab just as long as the command is /bin/bash /usr/local/bin/clearcache.sh >/dev/null 2>&1.

  • Now that you mention it, the command sudo rm -rf scares me. I'll think about using it in the future. – agamer569 Apr 11 '18 at 0:42
  • I don't see where the question you linked corroborates the warning that you gave. – Joseph Sible-Reinstate Monica Apr 11 '18 at 3:10
  • @agamer569 It isn't sudo rm -rf (its rm -rf), but having a healthy fear of it is a good thing. But the reason I don't use sudo is because it is unnessecary. Don't use sudo when not needed. – fosslinux Apr 11 '18 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.