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.

Permanent Delete vs. Send to Trash/Recycle Bin

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?

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.

Schedule autotrash to Automatically Run Every Time You Log in to Your Desktop

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

sudo apt update && sudo apt install autotrash


Enter your user’s password at the prompt.


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

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

nano ~/.xsessionrc

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

autotrash -d 30 &


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):

systemd-cat -t autotrash -p info autotrash -v -d 30 &


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



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.

Alexandru Andrei

Fell in love with computers when he was four years old. 27 years later, the passion is still burning, fueling constant learning. Spends most of his time in terminal windows and SSH sessions, managing Linux desktops and servers.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox