Safely Trim a Mounted Filesystem with fstrim

fstrim tells the kernel to discard unused blocks on a mounted filesystem, and a dry run lets you see the effect before you commit to it. Allow about five minutes for a single filesystem, plus time to check the storage layout. The examples use the util-linux command installed at /usr/sbin/fstrim; on this host it is util-linux 2.39.3, matching the installed manual page. A separate Homebrew copy earlier in PATH reports 2.41.3, so the explicit path keeps the examples tied to the packaged command.

1. Confirm the command and target

Start by checking the command and listing mounted filesystems without changing anything:

$ /usr/sbin/fstrim --version
fstrim from util-linux 2.39.3
$ findmnt -o TARGET,FSTYPE,OPTIONS
TARGET FSTYPE OPTIONS
/      ext4   rw,relatime

Your mount table will differ. Choose an exact target such as / or /srv, and check it before proceeding:

$ findmnt --target /srv
TARGET SOURCE    FSTYPE OPTIONS
/srv   /dev/...  ext4   rw,relatime

If findmnt reports no filesystem for the path, stop and correct the path. Do not substitute a device name, because fstrim needs the mounted filesystem context.

2. Preview the operation

Run a dry run with verbose output. This performs the discovery work but does not call the kernel's FITRIM operation:

$ sudo /usr/sbin/fstrim --dry-run --verbose /srv
/srv: 0 B (dry run) trimmed

The byte count is a preview, not a promise about the next real run. A non-zero value means the filesystem reports free ranges that could be passed down for discard. A zero value can be correct if there is nothing eligible, if the filesystem or storage stack has already dealt with the ranges, or if the target does not support the operation.

Checkpoint: proceed only when the target printed by the command is the filesystem you intended. The command normally needs elevated privileges, because it opens the mounted filesystem and requests a discard operation.

3. Trim one filesystem

After checking the dry-run result, run the same command without --dry-run:

$ sudo /usr/sbin/fstrim --verbose /srv
/srv: 12.3 GiB (132070244352 bytes) trimmed

The exact number depends on current free space and the device. With --verbose, fstrim reports bytes passed from the filesystem towards the storage device for potential discard. That is a maximum from the device's perspective: RAID geometry, LVM and other layers may reduce the actual ranges, and repeated runs can report similar potential bytes even though only sectors written since the previous discard need new work.

Warning: trimming is not a file deletion command, and it does not make deleted data readable again. It changes storage-layer discard state, so there is no file-level undo. If a run causes unusual I/O latency, stop repeating it and investigate the device, filesystem and storage stack rather than trying to reverse the operation.

4. Use a restricted range only when you need one

By default, fstrim searches from the beginning of the filesystem to its end. --offset selects the starting byte, and --length limits the search after that point. Use these options for a deliberate maintenance range, not as a guess about partition boundaries:

$ sudo /usr/sbin/fstrim --dry-run --verbose \
    --offset 1GiB --length 8GiB /srv
/srv: 0 B (dry run) trimmed

The installed manpage accepts binary suffixes such as KiB, MiB and GiB; the iB part may be omitted. If the requested length reaches beyond the filesystem, fstrim stops at the filesystem boundary. Keep the dry run while checking a range, then remove it only after you have confirmed the numbers and target.

5. Choose a safe multi-filesystem scope

Both can skip unsupported, read-only and read-only-mounted filesystems, but they can still cover more storage than you intended. Preview the exact scope first:

$ sudo /usr/sbin/fstrim --dry-run --verbose --fstab
/: 0 B (dry run) trimmed

Use --types ext4,xfs with --all or --fstab when you need an allow-list of filesystem types. A type list prefixed with no expresses forbidden types according to the mount type-pattern rules. An X-fstrim.notrim mount option can mark a filesystem to be skipped in the relevant fstab-driven selection.

A weekly trim is usually sufficient for desktop and server systems, according to the installed manual page. Running it repeatedly can hurt poor-quality SSDs, and each operation can impose a performance cost. Do not add a permanent timer merely because a one-off command succeeded. Check whether your distribution already manages the util-linux fstrim timer before changing service configuration.

6. Read failures and verify the status

For one target, status 0 means success and status 1 means failure. For --all, status 32 means every filesystem failed, while status 64 means some succeeded and some failed. Save the output when running a multi-filesystem command, so you can identify the affected mountpoints.

$ sudo /usr/sbin/fstrim --all --verbose
/: 0 B (dry run) trimmed
$ printf 'exit status: %s\n' "$?"
exit status: 0

The output above is illustrative of a successful result; the mount list and byte counts are host-specific. If the error says the operation is unsupported, check the filesystem type, mount mode, device path and layers such as LVM or RAID. --quiet-unsupported suppresses those diagnostics and is intended for service or cron use when the failure is known, not as a first troubleshooting step.

Do not run fstrim against an unverified mountpoint with sudo, and do not treat a successful status as proof that every lower storage layer discarded every byte. Keep the target-specific command in your maintenance notes so the next run can be compared with the previous one.

Done means