Can't format or delete partitions in pendrive because of GPT table error

This is what I got while trying to format a partition in my pendrive:

Error deleting partition /dev/sdb1: Command-line parted --script "/dev/sdb" "rm 1"' exited with non-zero exit status 1: Warning: /dev/sdb contains GPT signatures, indicating that it has a GPT table. However, it does not have a valid fake msdos partition table, as it should. Perhaps it was corrupted -- possibly by a program that doesn't understand GPT partition tables. Or perhaps you deleted the GPT table, and are now using an msdos partition table. Is this a GPT partition table?

Error: Both the primary and backup GPT tables are corrupt. Try making a fresh table, and using Parted's rescue feature to recover partitions.

(udisks-error-quark, 0)

I ran into this problem with writing onto an old drive that had been written as a live linux disk.

Solution if you just want to erase the disk /dev/sdX:

sudo gdisk /dev/sdX
o  

(agree to delete partitions)

w  

(agree to write partition table)

And then you have a perfectly blank drive. All that remains is to create a partition if you want to use it.

Note: Gdisk is a disk partitioning tool, and it can erase your disk if not careful. It is designed to create, modify, and erase partitions.


Chances are your disk originally contained a GUID Partition Table (GPT), but you created a new Master Boot Record (MBR) partition table over that with a tool that doesn't understand GPT. Because GPT takes up more space than MBR, this means that there's leftover GPT data, and parted is becoming confused because of that.

If this analysis is correct, then the easiest solution is to run FixParts on the disk. FixParts will offer to remove the leftover GPT data, and thereafter you should be able to use the disk normally. (You don't need to do anything else in FixParts; just tell it to remove the GPT data when it asks you about this and then exit.)

There is a caveat, though: It's also possible that you had a GPT disk but that something accidentally wrote an MBR partition table to the disk. In this case, you probably want to recover the GPT, not wipe it out! If this is the case, the solution is to run gdisk on the disk. It will detect the problem and ask if you want to use the GPT or MBR data. Tell it to use the GPT data, check that it's correct by typing p and comparing the output to whatever you believe to be correct, and then type w to save the changes.

Both FixParts (fixparts) and gdisk are part of the gdisk package in Ubuntu. It's usually not installed by default, so you may have to install it by typing sudo apt-get install gdisk.