LinuxCommandLibrary

fstrim

Discard unused blocks on SSD or other storage

TLDR

Trim unused blocks on all mounted partitions that support it

$ sudo fstrim [[-a|--all]]
copy

Trim unused blocks on a specified partition
$ sudo fstrim [/]
copy

Display statistics after trimming
$ sudo fstrim [[-v|--verbose]] [/]
copy

SYNOPSIS

fstrim [options] mountpoint | -a

PARAMETERS

-a, --all
    Trim all mounted filesystems that support the discard operation. Requires root privileges.

-o, --offset
    Byte offset to start trimming from within the filesystem.

-l, --length
    The number of bytes to trim, or 0 to trim to the end of the device.

-m, --minimum
    The minimum contiguous free range (in bytes) to discard. Smaller ranges will be ignored.

-v, --verbose
    Print the number of discarded bytes and other details during operation.

-q, --quiet
    Suppress verbose output. Overrides -v.

-s, --secure
    Perform a secure discard. This attempts to erase data on discarded blocks more thoroughly but is often slower and less commonly supported by all hardware.

-h, --help
    Display a help message and exit.

-V, --version
    Display version information and exit.

DESCRIPTION

The fstrim command is used on Linux systems to discard unused blocks on a mounted filesystem. It sends TRIM or DISCARD commands to the underlying storage device for blocks that are no longer in use by the filesystem. This operation is critically important for Solid State Drives (SSDs) and other thin-provisioned storage technologies.

Unlike traditional Hard Disk Drives (HDDs) that overwrite data, SSDs need to erase blocks before new data can be written to them. If the operating system doesn't inform the SSD which blocks are free, the SSD might perform unnecessary erase cycles on blocks it believes are still in use, leading to performance degradation over time and reduced lifespan.

fstrim helps maintain optimal SSD performance by notifying the drive about free blocks, allowing the SSD's internal garbage collection mechanisms to work efficiently. It can be run manually or as a scheduled service (e.g., via cron or systemd) to keep drives performing well.

CAVEATS

Running fstrim can be I/O intensive and might temporarily impact system performance, especially on large filesystems or older SSDs.

Not all filesystems or storage devices fully support the TRIM or DISCARD operation; fstrim will only work on those that do. Filesystems typically include ext4, XFS, Btrfs, and F2FS.

The -s (--secure) option performs a secure discard, which aims to permanently erase data from discarded blocks. This operation is often slower, less commonly supported by all hardware, and should be used with extreme caution as it involves data destruction.

fstrim requires root privileges to execute successfully.

SCHEDULING <B>FSTRIM</B>

While fstrim can be run manually, it is most commonly scheduled to run periodically using cron jobs or systemd timers (e.g., fstrim.timer on many distributions). This ensures that SSDs are regularly trimmed without requiring manual intervention, helping to maintain long-term performance and drive health. For most desktop users, the default weekly fstrim.timer is sufficient.

ONLINE DISCARD VS. OFFLINE <B>FSTRIM</B>

Some modern filesystems (like ext4 with the discard mount option, or Btrfs) can perform online, continuous discard operations. This means blocks are trimmed as soon as they are freed. While convenient, enabling continuous discard can sometimes lead to noticeable performance overhead and increased wear, especially on older SSDs. For this reason, many users and distributions prefer periodic offline fstrim runs over continuous online discarding.

HISTORY

The concept of TRIM originated from the need to improve performance and lifespan of Solid State Drives (SSDs). The TRIM command itself was standardized as part of the ATA Command Set to allow an operating system to inform a NAND flash SSD which data blocks are no longer in use and can be internally erased.

Linux kernel support for the TRIM/DISCARD operation was introduced gradually, primarily via the FITRIM ioctl. The fstrim utility was developed as a user-space command-line tool to expose this kernel functionality, making it accessible for manual execution or automated scheduling. Early usage often involved infrequent, full-disk trims, but with the advent of more advanced SSDs and filesystem features, continuous or frequent smaller trims (e.g., via systemd's fstrim.timer) became more common for maintaining optimal performance without significant overhead.

SEE ALSO

mount(8), lsblk(8), df(1), cron(8), systemd.timer(5)

Copied to clipboard