[HowTo] TRIM an external SSD

Difficulty: ★☆☆☆☆

Do you get the following message when you try to TRIM your external SSD?

the discard operation is not supported

We can easily overcome this by creating a udev rule in /etc/udev/rules.d

How to do it

  • Identify the VendorID and ProductID of your device.

    Type:

    lsusb
    

    in your terminal and locate your external SSD. In my case:

    Bus 002 Device 005: ID 04e8:61f5 Samsung Electronics Co., Ltd Portable SSD T5
    

    You’ll see an ID value, mine is 04e8:61f5 The VendorID is the first part and the second part is the ProductID.

  • Create a udev rule

    sudo nano /etc/udev/rules.d/50-usb-ssd-trim.rules
    

    And enter this rule:

    ACTION=="add|change", ATTRS{idVendor}=="04e8", ATTRS{idProduct}=="61f5", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"`
    

    Ensure to use your VendorID and ProductID! Save and exit the editor.

    The next time you plug in your device you can Trim it manually using the fstrim command:

    sudo fstrim -v /path/to/SSD/mountpoint
    

Once a week/month should suffice, depending on your usage.

8 Likes

There is one point missing:

If you use external SSD with encryption LUKS, but TRIM does not work because of LUKS.

Solution:

  1. Check lsblk --discard /dev/XXXX to show:
$ lsblk --discard /dev/sdc
NAME                                          DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sdc                                                  0      512B       4G         0
└─sdc1                                               0      512B       4G         0
  └─luks-79289eda-1aba-4799-adfa-ae3784460a80        0        0B       0B         0

Copy this LUKS ID, then:

  1. Enable trim for this LUKS
$ sudo cryptsetup --allow-discards --persistent refresh luks-79289eda-1aba-4799-adfa-ae3784460a80
  1. That works!

I would change it to /etc/udev/rules.d/50-usb-ssd-trim.rules that makes us easy to understand.

1 Like