38

How do you delete all partitions on a device from the command line on Linux (specifically Ubuntu)? I tried looking at fdisk, but it presents an interactive prompt. I'm looking for a single command, which I can give a device path (e.g. /dev/sda) and it'll delete the ext4, linux-swap, and whatever other partitions it finds. Essentially, this would be the same thing as if I were to open GParted, and manually select and delete all partitions. This seems fairly simple, but unfortunately, I haven't been able to find anything through Google.

44

Would this suffice?

dd if=/dev/zero of=/dev/sda bs=512 count=1 conv=notrunc
  • 4
    This will not delete the partitions. By deleting the partitions he meant to preserve the MBR and just empty the partition table. – Mircea Vutcovici Mar 23 '11 at 19:22
  • 2
    No, this appears to do exactly what I need. I don't really care if the data is still there. GParted shows that the partitions are gone after running this, and that's what I wanted. – Cerin Mar 24 '11 at 11:53
  • Mircea Vutcovici wasn't talking about your data, but about the bootstrap code in your MBR. That's now gone, because you've erased it along with the 4 primary entries from the MBR-style partition table. – JdeBP May 17 '11 at 11:22
  • 1
    Don't forget to unmount the driver, otherwise it won't work. – OrangeTux Dec 6 '13 at 14:47
  • This also works great if you've used ZFS on a drive and are repurposing it for something else. Neither a standard Windows or Linux partitioning and format will get rid of the ZFS labels on it which can cause you major problems. – Tony Maro Nov 6 '18 at 16:50
31

The wipefs program lets you easily delete the partition-table signature:

wipefs -a /dev/sda

From man wipefs

wipefs can erase filesystem, raid or partition-table signatures (magic strings) from the specified device to make the signatures invisible for libblkid.

wipefs does not erase the filesystem itself nor any other data from the device. When used without any options, wipefs lists all visible filesystems and the offsets of their basic signatures.

wipefs calls the BLKRRPART ioctl when it has erased a partition-table signature to inform the kernel about the change.

11

Quick and Dirty: use gparted to delete the partitions, or if you’re in a hurry:

dd if=/dev/zero of=/dev/[disk device] bs=512 count=1

This will zap the MBR of the drive (Data is still intact).

Alternatively:

dd if=/dev/zero of=/dev/[disk device]

to wipe the whole drive (write a single pass of zeros over everything. Not "secure" but usually good enough), or use a "disk shredder" tool for a secure wipe.

6

See man sfdisk, which is a non-interactive variant of fdisk. Other than that, you can delete the whole partition table with dd, as pk wrote.

6

You should be able to use parted for this aswell, although that may involve some scripting to loop through the partitions.

3

If we're talking about MBR-style partitions...

dd if=/dev/zero of=/dev/[disk device] bs=1 count=64 seek=446 conv=notrunc

Explanation:

dd

This standard command copies bytes from a source and writes them to a destination. It's the simplest flexible tool for this job.

if=/dev/zero

Here, we specify that we're reading from /dev/zero, which is a special device which emits NUL bytes--zeros.

of=/dev/[disk device]

Here, we specify which device we're writing to.

bs=1

dd thinks in terms of blocks. The default block size may be 512 bytes, 1024 bytes or 4096 bytes, depending on your system. However, we need to address things more precisely than that, so we tell dd to use a block size of 1 byte.

count=64

Here, we tell dd to write 64 blocks (or bytes, because of our bs=1 parameter), since the primary partition table consists of 4 16-byte partition entries, for a total of 64 bytes.

seek=446

The primary partition table within the MBR (so, not talking about GPT here) is located 446 bytes in, so we instruct dd to seek 446 bytes in before writing.

Extended partitions are generally created by using a primary partition slot to point at the extended partition table, so if we erase the 4 primary partitions, we effectively wipe the extended partition table as well; the OS won't be able to find it, so it won't be able to read and interpret it. (If you want to wipe the extended partition table, you'll need to know more about the operating system; different operating systems do extended partitions in different ways.)

2

I wanted to do the same thing (except in Slackware 14.2) but found I could not effect most of the solutions proposed here, with the most elaborate and well-documented solution creating new problems for making replacement partitions. That deleted the partition but some partitioning software apparently found the partition backups automatically.

I found f3probe (http://oss.digirati.com.br/f3) solved the problem of deleting all the partitions, quickly and easily, working with large capacity drives, and created exactly 1 partition spanning the whole drive, which was easy to delete.

It was also easy, from there to create new partitions, in a straight-forward way.

i.e.

f3probe --destructive --time-ops /dev/sdb
# Now we know only 1 partition exists on /dev/sdb
#    which is /dev/sdb1
#
# Unmount that partition
umount /dev/sdb1

#
# Delete that single partition
parted /dev/sdb rm 1

#
# Now you can create new partitions
# i.e. parted /dev/sdb mkpart primary fat32 1049K 15.8G
# 
# Update /etc/fstab before rebooting....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.