Filesystems & Storage

TRIM

/ TRIM = trim /

When you delete a file, the filesystem just marks its blocks free in its own bitmap - it does not wipe them, and it has no reason to tell the disk anything, because a normal disk does not care which sectors are in use. But an SSD desperately cares: its FTL needs to know which sectors hold dead data so it can erase and reuse that flash efficiently. Without being told, the SSD has no way to know a sector is now garbage and will keep faithfully preserving it. TRIM is the command that closes this gap - the filesystem tells the SSD 'these logical sectors no longer hold valid data; you may discard them.'

Here is why it matters mechanically. The FTL only ever sees reads and writes to logical sectors; a delete inside the filesystem is invisible to it. So when you free a file, the SSD still thinks all those sectors are live and dutifully copies their (now useless) contents around during garbage collection and wear leveling, doing pointless work and amplifying writes. A TRIM command (called discard in Linux, UNMAP in SCSI) lets the filesystem inform the drive that a range of logical sectors is now free, so the FTL can drop them from its accounting, skip relocating their data, and reclaim that flash sooner. This both speeds up future writes (more pre-erased space ready to go) and reduces wear. TRIM can be issued continuously (mount option discard, sending a TRIM on every delete) or, more commonly and more efficiently, in periodic batches via the fstrim command (often run weekly by a timer) which TRIMs all currently-free space at once.

Why it matters: TRIM is why a well-maintained SSD keeps its write performance over time instead of slowly degrading as the FTL runs out of known-free space to write into. The honest details: TRIM only helps SSDs and other flash/thin-provisioned storage - it is meaningless on a plain spinning disk, which does not care about free sectors. Continuous discard on every delete can add latency on some drives, which is why periodic fstrim is usually preferred. And TRIM is an advisory hint about free space, not a secure erase: a TRIMmed sector's data may or may not be immediately unreadable, so do not rely on TRIM to guarantee data destruction.

$ sudo fstrim -v / /: 41.2 GiB (44231901184 bytes) trimmed # tells the SSD which currently-free logical sectors it may discard, # so the FTL stops preserving dead data and reclaims that flash sooner

fstrim hands the SSD a batch list of free sectors so its FTL can stop preserving and start reclaiming them.

TRIM matters only for flash and thin-provisioned storage; on a spinning disk it is meaningless. It is a free-space hint, not a secure erase - do not assume TRIMmed data is instantly unrecoverable. Continuous discard can add per-delete latency on some drives, so a periodic fstrim is usually the better choice.

Also called
discardUNMAPfstrimTRIM 指令丟棄