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.
10 Answers
Would this suffice?
dd if=/dev/zero of=/dev/sda bs=512 count=1 conv=notrunc - 8This will not delete the partitions. By deleting the partitions he meant to preserve the MBR and just empty the partition table.Mircea Vutcovici– Mircea Vutcovici2011-03-23 19:22:08 +00:00Commented Mar 23, 2011 at 19:22
- 3No, 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– Cerin2011-03-24 11:53:09 +00:00Commented Mar 24, 2011 at 11:53
- 2Don't forget to unmount the driver, otherwise it won't work.OrangeTux– OrangeTux2013-12-06 14:47:42 +00:00Commented Dec 6, 2013 at 14:47
- 1
- 1To be fair, he said delete the partitions, not the partition table or data, so it wasn't really clear. But I think we knew he just wanted the parts not to register, which makes this the correct answer. To delete the data would be uneccesary. IF you want to zero it, thats a different story, that's deleting the data too.Brian Thomas– Brian Thomas2020-08-10 19:04:38 +00:00Commented Aug 10, 2020 at 19:04
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.
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.
- 1why is the second "not secure"?Herdsman– Herdsman2020-07-11 08:54:58 +00:00Commented Jul 11, 2020 at 8:54
- Not secure because it's possible that the disk's contents could be partially recovered by someone with physical access to it. A secure wipe writes multiple passes of random data over the whole disk.Alex Grounds– Alex Grounds2021-03-16 20:51:15 +00:00Commented Mar 16, 2021 at 20:51
Use improved non-interactive version of fdisk, which is sfdisk
To erase partition table use this command:
sfdisk --delete /dev/sda Note: no confirmation will be thrown, the partitions will be deleted instantly and forever!
At the end of the execution, it will throw output like this:
The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. - I think it's worth making it clear that after running this command there won't be any confirmation prompts whatsoever, the partition will be deleted instantly.red-o-alf– red-o-alf2022-09-08 01:01:36 +00:00Commented Sep 8, 2022 at 1:01
- 1
- This is what I was looking for through the forest of fdisk commands and wrong ChatGPT answers!Sridhar Sarnobat– Sridhar Sarnobat2024-07-09 23:40:49 +00:00Commented Jul 9, 2024 at 23:40
- Wow, took 30 mins for a 1TB drive.Sridhar Sarnobat– Sridhar Sarnobat2024-07-10 00:10:39 +00:00Commented Jul 10, 2024 at 0:10
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.
- It may be worth to mention that e.g.
parted /dev/mmcblk0 --script mklabel gptdeletes all existing partitions. For me it wasparted /dev/mmcblk0 --script mklabel msdosgrenix– grenix2022-01-28 11:30:08 +00:00Commented Jan 28, 2022 at 11:30
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.)
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.... Using gparted from petalinux on EMMC for me it was
parted /dev/mmcblk0 --script mklabel msdos Here the corresponding help output
# parted /dev/mmcblk0 --script help mklabel mklabel,mktable LABEL-TYPE create a new disklabel[ 6897.473870] mmcblk0: (partition table) LABEL-TYPE is one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop 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 with Error: /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
This can be done with one simple parted command. Instead of deleting each partition individually, just over-write the partition table with a new one:
sudo parted /dev/sdX mklabel gpt This creates a new (empty) GPT partition table on the block device corresponding to /dev/sdX. If you prefer to have the older MBR (Master Boot Record), use:
sudo parted /dev/sdX mklabel msdos Documentation
The documentation for the mklabel command
mklabel label-type
Creates a new disk label, of type
label-type. The new disk label will have no partitions. This command (normally) won’t technically destroy your data, but it will make it basically unusable, and you will need to use the rescue command to recover any partitions.
Note: “disk label” is another term for the partition table (sometimes called a partition map).
Example
I had a USB flash drive containing four partitions:
$ sudo parted /dev/sdg print Model: Generic Flash Disk (scsi) Disk /dev/sdg: 62.9GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 32.8kB 1482MB 1482MB ISO9660 hidden, msftdata 2 1482MB 1493MB 10.4MB fat16 Appended2 boot, esp 3 1493MB 1493MB 307kB Gap1 hidden, msftdata 4 1493MB 62.9GB 61.4GB Instead of deleting each of these partitions individually, I created a new partition GPT table:
$ sudo parted /dev/sdg mklabel gpt Warning: The existing disk label on /dev/sdg will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? y Information: You may need to update /etc/fstab. The following command shows that there are now no partitions on the disk:
$ sudo parted /dev/sdg print Model: Generic Flash Disk (scsi) Disk /dev/sdg: 62.9GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: