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.
Would this suffice?
The
wipefs
program lets you easily delete the partition-table signature:From
man wipefs
Quick and Dirty: use
gparted
to delete the partitions, or if you’re in a hurry:This will zap the MBR of the drive (Data is still intact).
Alternatively:
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.
Use improved non-interactive version of fdisk, which is sfdisk
To erase partition table use this command:
Note: no confirmation will be thrown, the partitions will be deleted instantly and forever!
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.You should be able to use parted for this aswell, although that may involve some scripting to loop through the partitions.
If we're talking about MBR-style partitions...
Explanation:
This standard command copies bytes from a source and writes them to a destination. It's the simplest flexible tool for this job.
Here, we specify that we're reading from
/dev/zero
, which is a special device which emitsNUL
bytes--zeros.Here, we specify which device we're writing to.
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 telldd
to use a block size of 1 byte.Here, we tell
dd
to write 64 blocks (or bytes, because of ourbs=1
parameter), since the primary partition table consists of 4 16-byte partition entries, for a total of 64 bytes.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.)
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.
Using gparted from petalinux on EMMC for me it was
Here the corresponding help output
BTW: concerning
wipefs -a /dev/sda
(and maybe some other solutions here)Using this made commands like
parted /dev/mmcblk0 --script mkpart primary fat32 1MiB 2049MiB
for me to fail withError: /dev/mmcblk0: unrecognised disk label
. The mklabel command of gparted is 'repairing' this condition resp. seems to be a good preparation for follwing steps.See also https://unix.stackexchange.com/questions/200582/scripteable-gpt-partitions-using-parted