If you are using a Solid State Drive (SSD), you should know that you shouldn’t run any defragmentation or free space consolidation software on it. So how do you clean up your SSD and free up the empty space? TRIM is the command we use to inform the OS to do the cleaning job. Windows 11 allows you to manually run TRIM through the fsutil command. But what about Ubuntu? How can you enable TRIM for SSD in Ubuntu?
Note: The following steps will not work if you have encrypted your partition.
Creating a TRIM Cron Job for Your SSD
- To make sure that the SSD in your computer supports TRIM, open a terminal and type:
sudo hdparm -I /dev/sda
If Ubuntu is not installed in the first partition of the SSD, change sda
to reflect the partition where Ubuntu is residing. You can do this by running lsblk
.
- Scroll down the list until you see the “Enabled Supported” section. Scroll down further, and if you see something like “Data Set Management TRIM supported (limit 8 blocks),” then TRIM is supported for your SSD.
- Test whether the TRIM function is working in Ubuntu. In the terminal, type:
sudo fstrim -v /
This will clean up the root partition of the SSD. If successful, you should see something that resembles the following image.
- Set a cron job for the OS to send the TRIM command once everyday.
sudo nano /etc/cron.daily/trim
- Paste the following code into the blank area:
#!/bin/sh fstrim -v /
- If your HOME directory in located on another partition, you can add an additional line to the end of the above code:
fstrim -v /home >> $LOG
- If you want to save the output to a log file, use the following code instead:
#!/bin/sh LOG=/var/log/trim.log echo "*** $(date -R) ***" >> $LOG fstrim -v / >> $LOG fstrim -v /home >> $LOG
- Save (Ctrl + O) and exit (Ctrl + X).
- Make the cron job executable:
sudo chmod a+x /etc/cron.daily/trim
Good to know: cron is a great utility that can help you automate tasks in Linux. Learn how it works and how you can use it for your own programs.
Using the Discard Option in fstab
Aside from manually invoking fstrim
, you can also configure your system to continuously “discard” any unused empty space on your disk. This can be helpful if you are using a laptop and do not want to leave your computer on all the time.
- Check your root partition’s UUID value:
ls -l /dev/disk/by-uuid
- Open the “/etc/fstab” file using your favorite text editor. In my case, I am using GNU Nano:
sudo nano /etc/fstab
- Find your root partition’s “/etc/fstab” entry by pressing Ctrl + W, then typing its UUID value.
- Go to your root partition’s entry and type
discard,noatime,
before the “errors=remount-ro” option. For example, my “/etc/fstab” file has the following root partition entry:
# <file system> <mount point> <type> <options> <dump> <pass> UUID=xxx / ext4 discard,noatime,errors=remount-ro 0 1 [...]
- Save your new “/etc/fstab” file by pressing Ctrl + O, then Ctrl + X.
- Lastly, reboot your machine to apply your new settings.
FYI: while TRIM will delete any free space on your SSD, that alone will not protect you from data recovery programs. Learn how you can securely erase your data using only simple command line tools.
Frequently Asked Questions
Can I use both fstrim and discard on my SSD at the same time?
Yes. However, doing that will neither result in any noticeable improvement nor extend the life of your SSD, as both fstrim and discard use similar TRIM routines when they delete unused, free blocks.
Knowing that, fstrim and discard differ in one key point: frequency. The former accumulates all unused blocks before “freeing” them, while the latter “frees” a block as soon as it becomes unused. This means that discard will always take more system resources and disk IO over fstrim.
My /etc/fstab file does not have a UUID for its root partition.
This is most likely due to your system using either a hardware RAID card or a logical volume management (LVM) utility. By default, TRIM will only work on systems that use traditional disk partitions and file systems such as ext4.
Regardless, some LVM programs provide a handy option for doing TRIM. In Ubuntu, you can add issue_discards = 1
to your “/etc/lvm/lvm.conf” file to automatically TRIM any unused, free blocks.
Is it possible to change the frequency of my fstrim command?
Yes. By default, Linux provides multiple timer files that allow you to finely tune when you want to use fstrim. For example, you can run sudo mv /etc/cron.daily/trim /etc/cron.weekly/trim
to change your TRIM frequency from every day to every week.
Image credit: Unsplash. All alterations and screenshots by Ramces Red.
Our latest tutorials delivered straight to your inbox