LinuxCommandLibrary

blkdiscard

Discard device blocks, securely erase data

TLDR

Discard all sectors on a device, removing all data

$ blkdiscard [/dev/device]
copy

Securely discard all blocks on a device, removing all data
$ blkdiscard [[-s|--secure]] [/dev/device]
copy

Discard the first 100 MB of a device
$ blkdiscard [[-l|--length]] [100MB] [/dev/device]
copy

SYNOPSIS

blkdiscard [-f|-I|-n|-o offset|-l length|-s step|-v|-V|-h] device

PARAMETERS

-f, --force
    Perform calculations in userspace; implies --no-verify

-I, --ignore-check
    Ignore BLKDISCARDZEROES check (refuses discard if device returns zeros)

-n, --no-act
    Dry-run: do everything except actual discard

-o, --offset <offset>
    Starting byte offset for discard (default: 0)

-l, --length <length>
    Discard length in bytes (default: device size from offset)

-s, --step <step>
    Discard step size in bytes (default: device granularity)

-v, --verbose
    Display verbose progress information

-V, --version
    Print version and exit

-h, --help
    Show help and exit

DESCRIPTION

blkdiscard is a utility that sends discard (TRIM or UNMAP) requests to a block device, informing the underlying storage (typically SSDs or thinly-provisioned volumes) which sectors are no longer in use. This enables the drive's garbage collection, improving performance and reclaiming space.

It operates directly on block devices like /dev/sda, bypassing the filesystem. Use it on unmounted devices or raw partitions to avoid data corruption. The command reads the device's discard granularity and alignment, then issues BLKDISCARD ioctl requests.

Key benefits include preventing write amplification on SSDs, optimizing thin provisioning on LVM or ZFS, and maintaining drive health. It's more targeted than fstrim, which scans mounted filesystems. However, not all devices support discard; check with blockdev --getdiscardzeroes.

By default, it discards from offset 0 to device end, in steps matching granularity. Options allow precise control over range, step size, and verification.

CAVEATS

Extremely dangerous on mounted filesystems or active data; risks total data loss. Verify device supports discard. Avoid on non-SSD/HDD thin-provisioned storage without confirmation. Kernel may limit max discard range.

EXAMPLE

blkdiscard /dev/sda1
blkdiscard -o 1G -l 10G -s 1M /dev/nvme0n1

VERIFICATION

Check support: blockdev --getss --getsize64 --getdiscardzeroes /dev/sdX

HISTORY

Added in util-linux 2.22 (2012); evolved with discard support in Linux 2.6.33+. Maintained by util-linux project for block device management.

SEE ALSO

fstrim(8), blockdev(8), hdparm(8), lsblk(8)

Copied to clipboard