TRIM an SSD in Linux

Last Update: November 8th, 2022
Article ID: 725946

Trim Support in Linux

Modern Linux distributions support the TRIM and DEALLOCATE commands for SATA SSDs and NVMe SSDs respectively, for simplicity we will refer to these two commands as Trim in this article.

These commands are filesystem dependent and supported on the following file systems:

File System Trim Support
Btrfs Yes
Ext4 Yes
XFS Yes
JFX Yes
EXT3 No
NTFS-3G Yes (Periodic Only)
VFAT Yes

When a file is no longer needed by the file system, the TRIM command can be sent to the SSD to help the built-in garbage collection utility determine which memory locations need to be maintained and which can be ignored. Maintaining unnecessary memory locations takes time and can slow down read and write access to the drive.

Most current Linux distributions use fstrim.service and fstrim.timer to coordinate periodic SSD maintenance with TRIM. However USB connected SSDs do not automatically have TRIM enabled, instead requiring some manual configuration. The TRIM command can be issued manually using the fstrim command, for example the following code will run Trim on compatible file system mounted to /media/user/USBSTORAGE. Please note, all code examples in this document require root permissions unless otherwise noted.

fstrim -v /media/user/USBSTORAGE

Ubuntu 20.04 LTS automatically mounts USB storage to /media/{username}/{storage volume name} while Fedora 34 automatically mounts USB storage to /run/media/{username}/{storage volume name}. The fstrim command can be run using these automatic mount points after enabling Trim for the USB storage drive.

Verify Trim Compatibility on the External SSDs

Our first step is to check for Trim support on the SSD, this requires installing the sg3 utilities package:

Ubuntu 20.04 LTS

sudo apt install sg-utils

Fedora 32

sudo dnf install sg_utils

Next we can run the sg_vpd command to check for unmap support, in the following command /dev/sdX is the path to the block device for the USB connected SSD (this can be found by checking the `dmesg` output after connecting the drive or with the `lsblk` command)

sudo sg_vpd -a /dev/sdX | grep "Unmap"

If the command returns the following line then we can enable Trim on this device.

Unmap command supported (LBPU): 1

Enable Trim on the External SSD: Temporarily

We can enable Trim on the SSD until the drive is either disconnected, or the computer is restarted with this command, with sdX again being the block device for the USB connected SSD:

echo unmap | sudo tee /sys/block/sdX/device/scsi_disk/*/provisioning_mode

To enable Trim on Plugable's SSD docks, adapters, and enclosures on every connection

For our USBC-SATA-V: USB-C Vertical Hard Drive Dock

Our USBC-SATA-V USB-C Vertical Hard Drive Dock supports both TRIM and SMART for docked SATA SSDs.

Run the following command to enable TRIM when the enclosure is connected to the computer:

echo 'ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="1153", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"' | sudo tee --append /etc/udev/rules.d/10-uas-discard.rules

USBC-SATA24: 2.5-inch SATA SSD TRIM

Our USBC-SATA24 USB-C 2.5-inch SSD and HDD Adapter supports both TRIM and SMART for connected SATA SSDs.

Run the following command to enable TRIM when the enclosure is connected to the computer:

echo 'ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="55aa", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"' | sudo tee --append /etc/udev/rules.d/10-uas-discard.rules

USBC-NVME: USBC-NVME Enclsoure

Our USBC-NVME Tool Free M.2 NVMe SSD Enclosure supports both TRIM and SMART for installed NVMe SSDs.

Run the following command to enable TRIM when the enclosure is connected to the computer:

echo 'ACTION=="add|change", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="9210", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"' | sudo tee --append /etc/udev/rules.d/10-uas-discard.rules