Manually trim solid-state drivers (SSDs) in Linux

By trimming your SSD, you may permanently lose your data. Please do with your own risk.

Linux has a build in tool blkdiscard to trim SSDs. To trim a entire disk, use command(at your caution):

blkdiscard /dev/sdb

How to test? First, write some content to the disk:

echo test > /dev/sdb

Then, check the content on the disk:

dd if=/dev/sdb of=/dev/stdout bs=1024 count=1

Then, trim the disk:

blkdiscard /dev/sdb

Check the content once again. The content on the disk should have been removed.

How the trim command been send? Checking out the source code of blkdiscard, we can find blkdiscard send ioctl command BLKSECDISCARD (if secure discard flag is on) or BLKDISCARD to the disk.

To trim your SSD with your own code, checkout this post.