How to use the command blkdiscard (with examples)
- Linux
- December 25, 2023
The blkdiscard
command is used to discard device sectors on storage devices, particularly useful for solid-state drives (SSDs). By discarding sectors, it effectively removes all data from the device, making it suitable for erasing sensitive information or preparing the device for reuse or disposal.
Use case 1: Discard all sectors on a device, removing all data
Code:
blkdiscard /dev/device
Motivation: This use case is useful when you want to wipe out all data on a storage device and start with a clean slate. It can be helpful when you’re selling or giving away a device or if you want to securely erase sensitive information.
Explanation:
blkdiscard
: The command itself, used to discard device sectors./dev/device
: The device parameter specifies the target storage device that you want to discard the sectors from.
Example output:
discarded sectors: 98765432
In this example, 98,765,432 sectors have been discarded on the specified device.
Use case 2: Securely discard all blocks on a device, removing all data
Code:
blkdiscard --secure /dev/device
Motivation: Securely discarding blocks ensures that the data cannot be easily recovered. This use case is appropriate when dealing with highly sensitive information that must be destroyed beyond recovery.
Explanation:
blkdiscard
: The command itself, used to discard device sectors.--secure
: This flag enables secure mode, ensuring that the discarded blocks are overwritten with zeros or random data to make recovery difficult./dev/device
: The device parameter specifies the target storage device that you want to discard the sectors from.
Example output:
securely discarded sectors: 12345678
In this example, 12,345,678 sectors have been securely discarded on the specified device.
Use case 3: Discard the first 100 MB of a device
Code:
blkdiscard --length 100MB /dev/device
Motivation: There may be situations where you don’t want to discard the entire device but only a specific portion. For instance, if there is unwanted or corrupted data at the beginning of the device, you can discard the first few sectors to get rid of it.
Explanation:
blkdiscard
: The command itself, used to discard device sectors.--length 100MB
: This option specifies the length of the sectors to discard. In this case, it’s set to 100 megabytes./dev/device
: The device parameter specifies the target storage device that you want to discard the sectors from.
Example output:
discarded sectors: 524288
In this example, 524,288 sectors (equivalent to 100 megabytes) have been discarded at the start of the specified device.
Conclusion:
The blkdiscard
command is a useful tool for discarding device sectors on storage devices. Whether you want to remove all data, securely erase information, or discard a specific portion, the blkdiscard
command provides a convenient and efficient way to achieve these goals. Just be cautious when using it, as discarded sectors cannot be recovered, and data loss is irreversible.