How to Automatically Delete Old Files from Trash on Linux

It’s almost certain that at one point in your life you will delete the wrong file or directory. Or you will intentionally erase a file, only to realize later that you need it again. Most of you may be thinking, “Been there, done that!”

But there’s a better way to get rid of data you don’t need anymore. And it’s something that is implemented in Windows 10, some of the newer smartphones and maybe some desktop environments included in Linux distributions.

However, the adoption rate of this system is still very low, and most users are not really aware of how useful it can be. It’s understandable since it’s one of those features that you can’t appreciate until the day it saves you.

Long story short: both of these options suck. Permanently deleting a file presents the obvious problem mentioned earlier. What if you later realize that you need the data? There’s no easy way to get that back, especially on ext4 filesystems. (It’s somewhat easier to undelete on Windows’ NTFS filesystem.)

It’s true that on most days you’ll zap some files and never need them again. But on a sunny day you will feel like cleaning up your computer.

The desktop is messy, and you see a bunch of unnecessary spreadsheets lying around. You’ve printed these and gave them to your boss, so you see no reason why you will ever need them again. You erase everything, and the desktop is once again clean and nicely organized. Later that day you get a call from your boss. He lost the files, he lost the printed versions, he needs last month’s spreadsheets. Guess who will have to become a data recovery expert overnight?

trash-is-full

The Problem with Sending Files to the Trash

Let’s say you’re convinced now, even if the scenario doesn’t apply to you. Your company has good backup mechanisms in place, right?

But from now on, let’s say you renounce the sin of pressing Shift + Delete. Everything goes to the virtual Trash bin (or Recycle Bin if you’re on Windows). Need a file back? You can restore it in seconds.

That’s great! But what about freeing up space on your hard drive or SSD? Files in the trash still take up the same amount of storage space. You can empty the trash and free up space at any time – this is true. You probably won’t ever need the files that have been sitting there for weeks. But what about the last files you sent to the trash ten minutes ago? There’s no way to easily restore them now.

The conclusion is that every file needs to get “old” before it can be thrown out. If it sits in the virtual trash bin for a few weeks, and you didn’t need to restore it in that time, chances are you won’t ever need it again. On Linux there’s an utility called autotrash that can automate the process of purging only the old files from the trash.

Open your favorite terminal emulator, and install autotrash with the following command:

autotrash-install

Enter your user’s password at the prompt.

autotrash-confirm-install-min

Type “y” and press ENTER when asked if you want to continue.

Go back to your terminal emulator and type the following command:

In the text editor that opens, paste the line below:

autotrash-xsessionrc-schedule-autorun

This will delete files that have been sitting in the trash for over thirty days. You can adjust the number “30” as desired. The “&” sign at the end pushes the command to the background. This way, if there are thousands of files autotrash has to remove, you won’t have to wait for the command to finish until your desktop loads.

Press Ctrl + X to exit the nano editor, “y” to save the modified file, and Enter.

Optional: if you also want to log autotrash activity in the systemd journal (if available on your distribution), you can paste a different line in .xsessionrc (but remove the previous one first):

autotrash-xsessionrc-schedule-autorun-and-log-to-journal

Entries in the systemd journal, regarding autotrash, would look like the following image.

autotrash-logged-in-systemd-journal

At this point you can always send files to the trash instead of permanently deleting them. autotrash will automatically free up space on your disks. And you will always have a thirty-day window of opportunity to easily recover deleted data. You will certainly thank yourself one day for implementing this simple system.

Image credit: Recycle bin filled with crumpled papers by DepositPhotos

One comment

  1. If you keep your trash for 30 days, your boss will ask for some of them on the 35th day. If you keep them for 365 days, he want a copy on the 367th. Bottom line is that no matter how long you retain the Trash, your boss will want one of the files a few days after you empty the trash. Best solution is to backup before deleting.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.

Sponsored Stories