fstrim
Discard unused blocks on SSD or other storage
TLDR
Trim unused blocks on all mounted partitions that support it
Trim unused blocks on a specified partition
Display statistics after trimming
SYNOPSIS
fstrim [options] mountpoint
PARAMETERS
-a, --all
Trim all mounted filesystems on devices that support the TRIM operation.
-o, --offset offset
Specify the offset (in bytes) from the beginning of the filesystem to start trimming. Defaults to 0.
-l, --length length
Specify the number of bytes to trim. Defaults to the filesystem size.
-m, --minimum minimum
Minimum extent length (in bytes) to release. Smaller extents will be skipped. Useful for avoiding unnecessary TRIM operations on small files. Defaults to 0.
-v, --verbose
Enable verbose output, showing the range of discarded blocks.
-V, --version
Display version information and exit.
-h, --help
Display help text and exit.
mountpoint
The mount point of the filesystem to trim. If not specified when using -a, all mounted filesystems are trimmed.
DESCRIPTION
The fstrim command is a crucial tool for maintaining the performance of Solid State Drives (SSDs) and thin-provisioned storage.
It sends TRIM commands to the underlying storage, informing it of unused blocks within the filesystem. This allows the SSD or storage array to internally optimize its write operations, improving performance and extending its lifespan. Without fstrim, deleted data remains marked as used by the filesystem, potentially leading to write amplification and performance degradation over time.
The command can be run manually or scheduled using utilities like cron or systemd timers for periodic maintenance. It is particularly beneficial for filesystems like ext4, XFS, and Btrfs that support the TRIM operation.
Note that the filesystem must be mounted with the discard option, or fstrim must be run periodically. The main benefits of running fstrim are increased write speeds over time and an extended life of your SSD.
CAVEATS
Not all filesystems and storage devices support the TRIM operation. Using fstrim on unsupported devices may have no effect or even lead to data corruption in rare cases. Check your device and filesystem documentation before using it.
SCHEDULING FSTRIM
It is generally recommended to schedule fstrim to run periodically rather than continuously. A common practice is to use systemd timers or cron to run fstrim weekly or monthly. Excessive trimming can put unnecessary wear on the SSD, although modern SSDs are generally quite robust.
FILESYSTEM SUPPORT
fstrim relies on filesystem support for the TRIM operation. Filesystems like ext4 (since Linux kernel 2.6.33), XFS, Btrfs, and F2FS generally support TRIM. Older filesystems or configurations may not. Verify your filesystem's capabilities before using fstrim.
HISTORY
The fstrim command was introduced to address the performance degradation issues of SSDs over time. Prior to its existence, maintaining SSD performance was a manual and often complex process. The development of fstrim aimed to automate this process, making it easier for users to optimize their SSDs. It has become a standard tool for maintaining SSD performance on Linux systems since its inception.