Swap partitions provide dedicated disk space used as overflow memory when physical RAM is exhausted. On modern Linux systems with ample RAM or a preferred swap file, an old or oversized swap partition can waste disk space that could otherwise be used for data or additional volumes.
The kernel exposes active swap areas through swapon and /proc/swaps, while partition layout is managed by tools such as lsblk, fdisk, and parted. Swap partitions are normally enabled at boot through an entry in /etc/fstab that references the underlying block device or its UUID, so both runtime state and configuration must be updated when removing swap.
Before modifying partitions, backups of important data and configuration files reduce the risk of data loss. Removing the wrong partition or leaving a stale swap entry in /etc/fstab can lead to boot errors or memory pressure under heavy workloads. The procedure uses sudo for elevated privileges and standard disk utilities commonly available on most Linux distributions.
Steps to safely remove a swap partition in Linux:
- Check the currently active swap areas.
$ sudo swapon --show NAME TYPE SIZE USED PRIO /dev/sda3 partition 8191M 0 -2
If no rows appear, no active swap is present and the partition might already be unused.
- Display block devices and confirm which partition is marked as swap.
$ lsblk --fs NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 ext4 5b0f2c8a-bc3c-4b0f-9f0a-1d6f7be1c111 40.2G 35% / ├─sda2 ext4 9d9f0a5b-0e30-4e41-b60c-2f1b33f7c222 89.5G 20% /home └─sda3 swap 3c5b4f6e-7e3a-4f4d-8b7f-9e4a2e5f7333
Note the device name (for example /dev/sda3) or UUID of the swap partition for later steps.
- Deactivate the swap partition so the kernel stops using it.
$ sudo swapoff /dev/sda3
Disabling swap on a system with very limited RAM can cause applications to fail under heavy load.
- Verify that no swap area is still active.
$ swapon --show $ free -h total used free shared buff/cache available Mem: 15Gi 2.0Gi 11Gi 200Mi 1.9Gi 12Gi Swap: 0B 0B 0B
The absence of output from swapon --show and a zero-sized Swap row confirm that swap is fully disabled.
- Create a backup copy of the existing /etc/fstab configuration.
$ sudo cp /etc/fstab /etc/fstab.backup.$(date +%F)
An incorrect /etc/fstab entry can prevent a system from booting until the file is repaired from a recovery environment.
- Open /etc/fstab in a text editor with elevated privileges.
$ sudo nano /etc/fstab
Any preferred text editor such as vim or gedit can be used, provided it runs with root permissions.
- Locate the line that configures the swap partition and comment it out or remove it.
# /etc/fstab: static file system information. # # <file system> <mount point> <type> <options> <dump> <pass> UUID=5b0f2c8a-bc3c-4b0f-9f0a-1d6f7be1c111 / ext4 defaults 0 1 UUID=9d9f0a5b-0e30-4e41-b60c-2f1b33f7c222 /home ext4 defaults 0 2 UUID=3c5b4f6e-7e3a-4f4d-8b7f-9e4a2e5f7333 none swap sw 0 0 # Commented to remove swap partition #UUID=3c5b4f6e-7e3a-4f4d-8b7f-9e4a2e5f7333 none swap sw 0 0
Leaving a swap entry that points to a deleted partition can cause slow boot with timeout errors or a failed mount message on every startup.
- Save the modified /etc/fstab file and exit the editor.
In nano, use Ctrl+O to write the file, press Enter to confirm, and then use Ctrl+X to exit.
- Start fdisk on the disk that contains the swap partition.
$ sudo fdisk /dev/sda Welcome to fdisk (util-linux 2.38). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Selecting the wrong disk (for example /dev/sdb instead of /dev/sda) can result in deleting partitions from the wrong drive.
- Inside fdisk, list the existing partitions and confirm the number of the swap partition.
Command (m for help): p Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 sectors Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 84058111 84056064 40G 83 Linux /dev/sda2 84058112 268435455 184377344 88G 83 Linux /dev/sda3 268435456 285212671 167771216 8G 82 Linux swap / Solaris
Note the partition number (such as 3 for /dev/sda3) and confirm that its type is listed as swap.
- Delete the swap partition from inside fdisk using its partition number.
Command (m for help): d Partition number (1-3, default 3): 3 Partition 3 has been deleted.
Deleting the wrong partition can permanently remove data or make the operating system unbootable.
- Write the updated partition table to disk and exit fdisk.
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
After writing changes, the removed partition cannot be recovered without specialized tools and prior backups.
- Notify the kernel about the partition table change if required by the environment.
$ sudo partprobe /dev/sda
On many systems, udev and the kernel automatically detect the new partition layout, but running partprobe helps avoid stale views of the disk.
- Optionally reuse the freed space by extending an adjacent partition with parted or a filesystem-specific tool.
$ sudo parted /dev/sda print free Model: ATA Samsung SSD (scsi) Disk /dev/sda: 256GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 43.0GB 43.0GB primary ext4 boot 2 43.0GB 232GB 189GB primary ext4 232GB 256GB 24.0GB Free Space
Resizing partitions and filesystems without verified backups can cause irreversible data loss if interrupted or misconfigured.
- Confirm that the swap partition is gone and no swap area remains active.
$ lsblk --fs NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 ext4 5b0f2c8a-bc3c-4b0f-9f0a-1d6f7be1c111 40.2G 35% / └─sda2 ext4 9d9f0a5b-0e30-4e41-b60c-2f1b33f7c222 89.5G 20% /home $ swapon --show
The absence of a swap-typed partition in lsblk output and an empty swapon --show output confirm that the swap partition has been fully removed.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
