How to Wipe All Partitions on Linux (Ubuntu) Using Command Line Non-Interactively


3 views

When managing disk partitions on Linux, tools like fdisk and cfdisk require interactive sessions, which isn't ideal for automation or scripting scenarios. System administrators often need a non-interactive way to completely wipe partitions - whether for disk repurposing, system provisioning, or troubleshooting storage issues.

sgdisk: The Partition Wiping Powerhouse /h2>

The most efficient solution comes from gdisk package's sgdisk command. Unlike fdisk, it supports non-interactive operation through command-line arguments:


sudo sgdisk --zap-all /dev/sdX

To ensure all partition types (including GPT, MBR, and any filesystem signatures) are removed:


sudo sgdisk --clear \
            --mbrtogpt \
            --zap-all \
            /dev/sdX

dd /h2>

For complete disk sanitization (warning: destroys all data):


sudo dd if=/dev/zero of=/dev/sdX bs=1M count=100

This writes zeros to the first 100MB, effectively destroying partition tables.

After wiping, verify with:


sudo lsblk -f /dev/sdX
sudo blkid /dev/sdX

Here's a complete bash script for wiping multiple disks:


#!/bin/bash
for disk in /dev/sd{b..d}; do
    echo "Wiping $disk"
    sudo sgdisk --zap-all $disk
    sudo wipefs -a $disk
    sudo partprobe $disk
done
  • Always double-check device paths (/dev/sdX)
  • Consider using --pretend flag first to dry-run
  • For production systems, implement confirmation prompts

When performing disk repurposing or troubleshooting, you often need to completely wipe all existing partitions. Here's how to do it without interactive tools like fdisk or GParted.

sgdisk for Non-Interactive Wiping /h2>

The sgdisk command (from gdisk package) is perfect for scriptable partition manipulation:

# Install gdisk if needed
sudo apt install gdisk

# Wipe all partitions on /dev/sdX (replace X with your drive letter)
sudo sgdisk --zap-all /dev/sdX

# Verify the operation
sudo sgdisk --print /dev/sdX

parted /h2>

For systems without sgdisk, GNU parted can achieve similar results:

sudo parted /dev/sdX --script -- mklabel gpt

Some scenarios require additional steps:

# If disk is in use by LVM
sudo vgremove --force volume_group_name
sudo pvremove /dev/sdX

# For encrypted partitions
sudo cryptsetup erase /dev/sdX

Always confirm the operation completed successfully:

lsblk /dev/sdX
sudo blockdev --rereadpt /dev/sdX

Here's a complete bash script for safe partition wiping:

#!/bin/bash
set -e

DEVICE="/dev/sdX"

echo "Wiping all partitions on $DEVICE"
sudo sgdisk --zap-all $DEVICE
sudo partprobe $DEVICE
sudo wipefs -a $DEVICE

echo "Verifying..."
sudo sgdisk --print $DEVICE