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 context, we shall look into how to install the Autotrash utility to automatically empty the trash in Ubuntu.
How to install Autotrash on Ubuntu ?
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.
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.
2. Install Autotrash
Execute the below command in Terminal to install Autotrash utility on your system:
$ sudo snap install autotrash-unofficial
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
How to use 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.
How to 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 30 days:
$ autotrash -d 30
If you want to restore any deleted file, you can do that within 30 days.
How to 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 1024 MB of free space:
$ autotrash --min-free 1024
How to 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 2 GB.
If you request Autotrash to delete 1024 MB (M=1024), it will remove 2 GB.
$ autotrash --delete 1024
How to uninstall Autotrash from Ubuntu system ?
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.