Ubuntu

How to Automatically Empty the Trash in Ubuntu 20.04

When you delete a file in Linux, it is not immediately deleted from the system. Instead, it is moved to the Trash in case you need to restore it. Trash is the first place where you look for the accidentally deleted files. Because the file is not removed from the system, it continues to take space on the system and in the end, can fill up large storage space. Although we can empty the trash manually to avoid wasting system space, however, we often forgot to do such a simple task. Luckily, Linux OS offers a utility known as “Autotrash” that can automatically delete the data in the Trash based on the options you specify.

In this post, we will explain how to install the Autotrash utility to automatically empty the trash in Ubuntu. This way you can get rid of old and useless files and make room for important files.

Note: The commands discussed here have been tested on Ubuntu 20.04 LTS (Focal Fossa). You must have be root user or have sudo privileges to install Autotrash on your Linux system.

Installing Autotrash

Autotrash utility is by default not installed in the Ubuntu OS. However, it is available as a snap package that can be easily installed using the snapd.

Step 1: Install Snapd

First, install snapd on your system. Execute the below command to do so:

$ sudo apt install snapd

Enter sudo password, after that the installation of snapd will be started on your system. Once snapd is installed, you can use it to install Autotrash snap.

Step 2: Install Autotrash

Execute the below command in Terminal to install Autotrash utility on your system:

$ sudo snap install autotrash-unofficial

Once the Autotrash is installed you will see the below output:

installing autotrash

Step 3: Create an alias for Autotrash

After installation of Autotrash via snap, you will have to run every command by typing the complete snap name that is autotrash-unofficial. You can just use autotrash instead of complete snap name autotrash-unofficial by creating an alias as follows:

$ sudo snap alias autotrash-unofficial autotrash

The alias will support calling the autotrash-unofficial as just autotrash.

Note: In case, you want to remove the alias, execute the below command:

$ sudo snap unalias autotrash

To verify if the Autotrash has been successfully installed and to view its version, execute the below command in Terminal:

$ autotrash -V

autotrash version

Using Autotrash to Automatically Empty the Trash in Ubuntu

Autotrash allows you to automatically empty the trash based on certain options that you need to specify. It can automatically delete files based on age, size, and free space on the trash location.

Automatically Deleting Files older than x number of days

This is the most commonly used Autotrash functionality. The following command syntax can be used to automatically delete the files that exist in the trash for more than x number of days:

$ autotrash -d [number of days]

This will automatically delete the files that have been in the trash over the period you specified. For instance, the below command will delete the files that have been in the trash for more than 45 days:

$ autotrash -d 45

If you want to restore any deleted file, you can do that within 45 days.

Automatically Deleting files based on free-space left

Using the Autotrash utility, you can make sure a specific amount of disk space is free in the Trash location. Here is the syntax of the command:

$ autotrash --min-free [free-space in MB]

For instance, the below command will make sure there is a minimum of 512 MB of free space:

$ autotrash --min-free 512

Automatically Deleting M number of megabytes

Using Autotrash –delete option, you can allow it to automatically delete at least M megabytes, removing the oldest trashed items first. Here is the syntax of the command:

$ autotrash --delete [data size in megabytes]

This option removes the trashed entries, not the individual files. To explain it, consider you have an older trashed directory of 1 GB. If you request Autotrash to delete 512 MB (M=512), it will remove 1 GB.

$ autotrash --delete 512

Uninstalling Autotrash

In case you no longer need Autotrash on your system, you can easily remove it using the following command in Terminal:

$ sudo snap remove autotrash-unofficial

Enter password for sudo, after which Autotrash will be removed from your system.

In this post, we have explained how to install Autotrash on Ubuntu system. Now the Autotrash will automatically delete the files residing in the Trash based on the conditions you specified.

About the author

LinuxWays Team

LinuxWays Team

We create HowTos and Tutorials for Sys Admins. Visit us on <a href="https://linuxways.net/"LinuxWays.net