Linux SSD – How to TRIM/DISCARD a Whole Partition

linuxpartitioningssdtrim

My partition /dev/sda3 on an SSD drive doesn't contain any filesystem, but it contains garbage. How do I do a TRIM/DISCARD operation on the whole partition?

Best Answer

If your version of util-linux is new enough (September 2012), there is actually a purpose-built tool, blkdiscard, that is the best way to do this:

sudo blkdiscard /dev/sda3

But if you need compatibility to older Linux distro versions, read on... There are cases where hdparm/wiper.sh refuse to touch a volume because it's not a partition, so we need something beyond that.

The most supported way I've found is to take advantage of the fact that Linux swap volumes support DISCARD when they are enabled. The wipefs on the end is there so the volume isn't recognized as swap later.

D=/dev/sda3 ; mkswap $D && swapon -d $D && swapoff $D && wipefs -o 0xff6 $D

This issues the DISCARD on the majority of the device.