How to delete a partition with fdisk command on Linux

Featured Image of How to delete a Partition with FDISK and Various other Commands

FDISK (fixed or format disk) is used primarily for manipulating partitions on Linux and Unix systems. It allows for the creation and complete manipulation of partitions on your server. You can view information in a partition and create up to four partitions on a hard disk. FDISK commands allow you to control these partitions. This gives you full control over your server and lets you get more out of your technology. Learning all aspects of FDISK will benefit you and this article and many others on this site will help you learn every aspect of it. As you can imagine FDISK allows you to do many things but one of the most common things people need to know how to do is delete partitions with FDISK command. Deleting partitions is fairly simple and can be done with just a few commands.

Learning FDISK CommandsMost Common FDISK Commands

So, first it might be important to know that fdisk commands must be prefixed with sudo. On things that don’t use sudo, try the su- command. In order to bring up our list of partitions we want to type sudo fdisk -l. If you want to only list partitions on the first device you can enter:

sudo fdisk -l /dev/sda.
coderseye@ubuntu: sudo fdisk -l

Disk /dev/sda: 36.1 GB, 3614836480 bytes
255 heads, 64 sectors/track, 2810 cylinders, total 51943010 sectors
Units = sectors of 1 * 512 = 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006d013

Device Boot Start End Blocks ID System
/dev/sda1 * 2048 39845887 19921920 83 linux
/dev/sda2 39847384 58210081 1046529 5 Extended
/dev/sda5 39847384 58210081 1046529 82 Linux Swap
/ Solaris

In order to enter commands in fdisk you must be able to enter command mode. To enter command mode you need the name of a disk from the fdisk -l command. So to enter the command mode type sudo fdisk /name of disk. In command mode you use single letter commands; typing m will bring up all commands.

coderseye@ubuntu:~$ fdisk /dev/sda
Device contains neither a valid DOS partition table, nor sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0xef66215b99. 

Changes will remain in memory only, until you decide to write them. Previous contents will no longer be recoverable.

The number of cylinders for this dis is set to 38154.
There is nothing wrong with that, but it is larger than 1024, and could in certain setups cause problems with:
1. software that runs at boot time (e.g., old versions of LILO)
2. booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of aprtition table 4 will be corrected by w(rite)

command (m for help): 

(WARNING: Be careful re-partitioning drives with data; this wipes the Partition.)

Creating the PartitionHow to Create the FDISK Partition

Before you can delete a partition you need to know how to create a partition, right? So, to create a partition you use the n command and you create one of two type of partitions; logical (l) or primary (p). (Keep in mind that you can only have 4 primary partitions).

Deleting the PartitionHow to Delete the FDISK Partition

Now, to delete a partition on Linux you will first need to use a mount command in order to locate the mount point of the partition.

coderseye@ubuntu:~$ # mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode620)
tmpfs on /dev/shm type tmpfs (rw,rootcontect="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda5 on /home type ext4 (rw)
/dev/sda6 on /test type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/nfs/rpc_pipefs type rpc_pipefs (rw)

Using mount will show all currently mounted devices and their file systems. Use the mount command to mount the desired partition, and then use the umount command to unmount it. So you would enter unmount /(partition you want to unmount). You can also use p to show you a list of partitions. After you unmount the partition run the fdisk command and then use p to print the current file system. After this you should see the partition you would like to delete. So you will type d and then the number partition you want to delete. If you wanted to delete partition /dev/sda4 you would type d and then after would type 4.

command (m for help): d
Partition number (1-6): 6

Command (m for help): w
The partition table has been altered!

Calling ioctle() to re-read partition table.

WARNING: Re-read the partition table failed with error 16: Device or resources busy. 

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks Complete.


If kernel could not unmount the partition correctly then Error code 16 will appear and you will need to do a reboot to locate the new partition table. So, deleting a partition with FDISK is not as challenging as it may have seemed and the more you use Linux based servers and specifically the more you create and manipulate partitions with FDISK the easier it will be to use and the quicker you’ll be able to navigate. This allows complete control of your server and by learning more and more about FDISK you will be able to reap all the benefits your server can provide. Deleting partitions is just one of the many things learning how to use FDISK can provide you.