Disable and Remove Swap in Ubuntu
This guide explains how to completely disable and remove swap space from Ubuntu and other Debian-based Linux systems.
These instructions cover both swap partitions and swap files.
Before proceeding with swap removal, you should verify your current swap usage:
free -h
swapon --show
This setup was tested on Ubuntu 22.04 LTS, but it should work on other Debian-based systems.
Quick Removal Commands
For immediate swap deactivation, use one of these commands depending on your swap type:
For swap partition:
sudo swapoff /dev/sdb1 # Replace with your swap partition
For swap file:
sudo swapoff /swapfile
sudo rm /swapfile
Complete Removal Process
To thoroughly remove swap and prevent it from being reactivated on boot, follow these steps:
1. Identify active swap spaces
swapon --show
Example output:
NAME TYPE SIZE USED PRIO
/dev/sdb1 partition 14.9G 3.7M -2
2. Disable active swap
For swap partition:
sudo swapoff /dev/sdb1 # Replace with your swap partition
For swap file:
sudo swapoff /swapfile
sudo rm /swapfile
3. Remove swap entry from /etc/fstab
Edit the fstab file:
sudo nano /etc/fstab
Find and comment out the swap entry by adding # at the beginning:
# UUID=606c8e0e-f961-495b-98a4-1034ad079341 none swap defaults 0 0
or
# /swapfile none swap sw 0 0
4. Verify removal
Check that swap is no longer active:
free -h
swapon --show
These commands should show no active swap space if the removal was successful.
Additional Notes
- Always ensure you have sufficient RAM before disabling swap
- Consider these scenarios where removing swap might be beneficial:
- Systems running from SD cards with limited write cycles
- Production systems booting from read-only storage
- Temporary removal for swap resizing or type conversion
- Some systems might require swap for hibernation to work
- Consider reducing swappiness instead of completely removing swap
The swap removal process is reversible - you can always recreate a swap partition or file later if needed.
Resources
- Linux Manual Pages
- Linux Handbook: How to Disable Swap in Linux
- ServerFault: Best way to disable swap in Linux