How to Automatically Empty the Trash in Ubuntu

If you want to set your Ubuntu system to automatically take care of your system Trash over a particular period of time, you can make use of the command line utility called autotrash. Autotrash is a utility that looks in a FreeDesktop.org Trash folder for information on its contents and then purges a part of the trash depending on your specified options.

In this article, we will explain how to install, seek help on, and use the autotrash utility. We will elaborate on the procedure by presenting some examples that will help you with the widely used functions of this utility.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Install Autotrash through the Command line

We will install the autotrash command line utility through the Terminal application. You can launch the Terminal either through the system application launcher search or the Ctrl+Alt+T shortcut. Please note that only an authorized user can add/update, remove and configure software in Ubuntu.

Step1: Update your system’s Repository Index

In order to install the latest version of a software available in Ubuntu’s Internet repositories, your system’s repository index needs to be up-to-date.

Run the following command as sudo in order to update your system’s local repository index with that of the Internet.

$ sudo apt-get update

Update Ubuntu Repository

The system might ask you for the password of sudo. Enter the password and your command will take action.

Step 2: Install autotarsh

Now, you can install the autotrash utility by running the following command as sudo in your Terminal application:

$ sudo apt-get install autotrash

Install Autotrash

The system might prompt you with a Y/n option before starting the installation procedure. Please enter Y to continue.

Step 3: Verify autotrash installation (optional)

You can check the version number of your installed autotrash software and also verify if it is indeed installed on your system. Run the following command to do so:

$ autotrash --version

Or,

$ autotrash -V

Verify autotrash installation

You are now ready to use autotrash on your Ubuntu.

How to use autotrash? (with examples)

The autotrash help will tell you all you can do with this application. You can view the help as follows:

$ autotrash --help

Get help

For even more detailed information, you can view the manpage of autotrash by using the following command:

$ man autotrash

Now we will present some examples that will elaborate on how you can make the best use of this utility:

Example 1: Delete Files older than x number of days

This is the most widely used feature of autotrash. You can use the following command syntax to automatically remove the files residing in your system trash for x number of days:

$ autotrash -d [days]

For example, the following command will remove the files that have completed their 30 days in the trash can.

$ autotrash -d 30

Autotrash 30 days

This gives you enough time to restore any files that you might wish to, during 30 days.

The following usage of the command lets you apply this feature for not only your trashed files but also deleted files of all the users using the system:

$ autotrash -td [days]

Example:

$ autotrash -td 30

Example 2: Delete files based on free-space left

Max-free

You can only use autotrash to purge files if there is less than M megabytes of free space left at the trash location. As an example, if you set this to 1024, then autotrash will only start to work if there is less than 1GB of free space in the trash. Till that time, autotrash will just exit normally without scanning the Trash directory.

Example:

$ autotrash --max-free 1024 -d 30

Min-free

Similarly, you can use autotrash to make sure there is a minimum of M megabytes of free space.

Example:

$ autotrash --min-free 1024

You can even combine the two command to make sure that autotrash starts reading the trash if there is less than 2 GB of free space, and then keep a check on. The command will then remove files older than 30 days and also remove extra files to ensure that 1 GB of space is always available in the trash.

$ autotrash --max-free 2048 --min-free 1024 -d 30

Example 3: Delete M number of megabytes

You can use the utility to purge at least M number off megabytes, deleting oldest trash addition first. This feature uses trash entries rather than the individual files.This means that if your oldest trashed item is a 1GB directory, and you request at least 10MB to be removed (M=10), autotrash will remove 1GB.

$ autotrash --delete M

Example

$ autotrash --delete 1024

Now you do not need to worry about space wastage on your system because of the system trash The autotrash utility will automatically take care of clearing your unwanted files based on your defined criteria.