How to Empty Your Trash Automatically in Ubuntu with AutoTrash

Keep Ubuntu Clean With Autotrash Featured

Are you losing GBs of storage space because you forget to clear your trash? With AutoTrash, you can get Ubuntu to empty trash automatically on its own, based on the conditions you specify.

Installation of AutoTrash

You won’t find AutoTrash in many distributions’ repositories anymore. For most users, the easiest way to install it is through an unofficial snap version, available at the snap store.

If using Ubuntu, which in its latest versions already supports snaps by default, you can find the program in the software center by searching for “autotrash.”

Keep Ubuntu Clean With Autotrash Locate Snap

Select the “autotrash-unofficial” entry that will appear and install it by clicking the green Install button.

Keep Ubuntu Clean With Autotrash Install Snap

For those who prefer typing commands rather than clicking, you can do the same by firing up your favorite terminal and using:

If the program isn’t available through your distribution’s repositories but you also have a distaste for snaps, there is a solution. Since it’s a Python script, you can use pip to install autotrash with:

When you install it like that, for ease of use, make sure to include its location in your PATH variable.

Keep Ubuntu Clean With Autotrash Pip Install

Set an alias

If you installed AutoTrash through the simpler route of snap, you can try it by typing autotrash-unofficial in a terminal. Alternatively, you can set up an alias for easier access.

If you would prefer to type autotrash instead of autotrash-unofficial to run the tool, type the following in the terminal:

Keep Ubuntu Clean With Autotrash Snap Alias

Clean your Trash

AutoTrash comes with a list of parameters that allow you to customize how it will clear your trash.

Using -d, you can define a date threshold. Everything moved to the trash within the specified number of days will remain intact; AutoTrash will wipe out everything older. For example, to eliminate all files older than 10 days, use:

If you have ample free space, there is no point in trying to free up even more by removing older files. Instead of checking it yourself, though, you can specify a free space threshold with --max-free. The values are in megabytes.

If you want AutoTrash to exterminate everything older than ten days only if your free space has dived under 4 GB, you would use:

Since 1 GB = 1024 MBs, the above number translates to 4 GB (4 x 1024).

Keep Ubuntu Clean With Autotrash Max Free

You can have AutoTrash ensure that you always have at least 512 megabytes of free space available with:

You can change 512 to whichever value you prefer.

It’s best to always make sure the results of any action will be what you originally intended. You can have AutoTrash perform a test run that makes no actual changes to your data, using the --dry-run switch. This doesn’t come with any extra values. Include it as is at the end of your command to check its outcome. Thus, with this addition, the previous example would look like:

Keep Ubuntu Clean With Autotrash Min Keep Free

Automatic Purging

Having to run AutoTrash to purge the contents of your trash manually is far from optimal. Thankfully, it’s easy to have it run automatically. You can set up a cron job for that, but we think the following approach is even more straightforward.

Search for “startup” among your applications and open Startup Applications Preferences.

Keep Ubuntu Clean With Autotrash Startup App Prefs

Create an empty bash script in whichever way you prefer and enter your AutoTrash command. I created a folder called Scripts in my home directory and then an empty Bash script named autoautotrash.sh inside it.

Keep Ubuntu Clean With Autotrash Create Script

I entered my autotrash command, saved the changes (Ctrl + W), and exited Nano (Ctrl + X).

Keep Ubuntu Clean With Autotrash Enter Command

With your command in your script, make it executable by entering the following in your favorite terminal:

In my case, this command looks like:

Keep Ubuntu Clean With Autotrash Make Script Executable

Finally, return to the Startup Applications Preferences. Click on the Add button on the top right to create a new entry. In the window that will pop up, enter a name for your entry, your script as the command, and if you wish, a comment about what it does, which is useful for future reference.

Keep Ubuntu Clean With Autotrash Add Script To Startup

That’s it. From now on, whenever you log in to your desktop, your script will be one of the first things that runs, purging files from your trash and freeing up precious storage.

Keep Ubuntu Clean With Autotrash Script As Startup App

Now that you have emptied your trash, if you are still looking for more ways to clean up your Ubuntu machine, check this out.

Related:

Odysseas Kourafalos Odysseas Kourafalos

OK's real life started at around 10, when he got his first computer - a Commodore 128. Since then, he's been melting keycaps by typing 24/7, trying to spread The Word Of Tech to anyone interested enough to listen. Or, rather, read.

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.