How to Enable TRIM For SSD in Ubuntu

A photograph of two NVMe SSD cards.

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

  1. To make sure that the SSD in your computer supports TRIM, open a terminal and type:
sudo hdparm -I /dev/sda
A terminal window showing the output of the hdparm command.

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.

A terminal window showing all the disks attached to the machine.
  1. 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.
A terminal window showing the TRIM flag available for the demonstration SSD.
  1. 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.

A terminal window showing the first fstrim command for the SSD.
  1. Set a cron job for the OS to send the TRIM command once everyday.
sudo nano /etc/cron.daily/trim
  1. Paste the following code into the blank area:
#!/bin/sh
fstrim -v /
  1. 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
  1. 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
A terminal window showing a complete cron script for fstrim.
  1. Save (Ctrl + O) and exit (Ctrl + X).
  1. Make the cron job executable:
sudo chmod a+x /etc/cron.daily/trim
A terminal window showing the trim script with correct permission bits.

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.

  1. Check your root partition’s UUID value:
ls -l /dev/disk/by-uuid
A terminal window showing the symbolic link between UUID and the SSD's partitions.
  1. Open the “/etc/fstab” file using your favorite text editor. In my case, I am using GNU Nano:
sudo nano /etc/fstab
  1. Find your root partition’s “/etc/fstab” entry by pressing Ctrl + W, then typing its UUID value.
A terminal window showing the default /etc/fstab file for the demonstration system.
  1. 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 
[...]
A terminal window showing the modified /etc/fstab file.
  1. Save your new “/etc/fstab” file by pressing Ctrl + O, then Ctrl + X.
A terminal window showing the save prompt for GNU Nano.
  1. Lastly, reboot your machine to apply your new settings.
A terminal window showing the reboot command for the demonstration system.

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.