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 [options] device

PARAMETERS

device
    The path to the block device (e.g., /dev/sda1, /dev/nvme0n1, /dev/sdb).

-o, --offset bytes
    Specifies the starting offset in bytes from the beginning of the device where discarding should begin. The default is 0.

-l, --length bytes
    Specifies the number of bytes to discard. By default, the entire device or the remaining part from the specified offset is discarded.

-s, --secure
    Performs a secure discard, if supported by the device. This aims for a more thorough erasure, attempting to ensure data is irreversibly destroyed according to device specifications. Not all devices support this level of security.

-z, --zeroout
    After discarding, zero out the blocks. This option is only effective if the device supports RZAT (Return Zero After Trim) or similar capabilities, meaning the device guarantees to return zeros on subsequent reads of discarded blocks.

-p, --pb-size bytes
    Specifies the physical block size of the device in bytes. This can be used to ensure alignment with the device's internal block structure for optimal performance. The default is usually determined automatically by the kernel.

-f, --force
    Force the discard operation even if the device is mounted. Use with extreme caution as this can severely corrupt the filesystem and lead to irreversible data loss.

-q, --quiet
    Suppress any output messages except for errors.

-v, --verbose
    Display more detailed output during the operation.

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

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

DESCRIPTION

The blkdiscard command is a utility used to discard (or TRIM/UNMAP) specified block ranges on a block device. This operation informs the underlying storage device (such as an SSD, eMMC, or thin-provisioned LUN) that these data blocks are no longer in use by the operating system and can be internally reclaimed. This is crucial for maintaining the performance and longevity of SSDs by allowing wear leveling and garbage collection, and for efficient space management in thin-provisioned storage. Unlike commands like dd or shred which overwrite data, blkdiscard does not write any data to the device; instead, it sends a command to the device controller indicating that the specified block ranges are free. This makes it a much faster operation for mass 'erasure' on compatible hardware. It is typically used on unmounted partitions or entire disks. For discarding free space on *mounted* filesystems, fstrim(8) is the appropriate command.

CAVEATS


Extreme Data Loss:
blkdiscard permanently discards data. Once blocks are discarded, the data they contained is typically unrecoverable. Use this command with the utmost care and always double-check the target device.

Mounted Devices: Never use blkdiscard on a mounted filesystem unless you fully understand the severe risk of filesystem corruption and irreversible data loss. The -f (force) option can override this safety mechanism, but it is highly discouraged for active filesystems. For discarding free space on *mounted* filesystems, the dedicated utility fstrim(8) is the correct and safe choice.

Device Support: blkdiscard relies on the underlying storage device supporting TRIM/UNMAP commands. If the device does not support this functionality, the command will either fail, appear to succeed but have no effect on internal data reclamation, or potentially return an error.

Secure Discard: The effectiveness of the --secure option depends entirely on the device's implementation and how it interprets the secure erase command. There is no universal guarantee of cryptographic erasure. For highly sensitive data, physical destruction or certified data sanitization methods might be necessary for absolute assurance.

HISTORY

The blkdiscard command is part of the util-linux project, a collection of essential utilities for Linux systems. It was introduced to provide a direct and low-level mechanism for managing block discards, which became increasingly important with the widespread adoption of Solid State Drives (SSDs) and thin-provisioned storage solutions. It leverages the Linux kernel's BLKDISCARD ioctl, allowing userspace applications to explicitly send discard (TRIM/UNMAP) commands to block devices. Its development reflects the need for tools that can efficiently interact with modern storage hardware capabilities.

SEE ALSO

fstrim(8), wipefs(8), dd(1), shred(1), mkfs(8)

Copied to clipboard