How to Remove Swap File From Ubuntu
A beginner's guide to show the steps for safely and properly removing swap file from an Ubuntu system.
The swap file is used when your system is running out of memory (RAM) and will use some portion of your disk to keep the processes running.
Though it's a good idea to always have some swap on your system. But if you have more than enough system memory and want to save some gigs from your main drive and in that case, you may remove the swap file from Ubuntu.
And in this tutorial, I will show you how to remove the swap file step by step.
Remove the swap file from Ubuntu
I hope you know what a swap file is. Unlike the swap partition, it is just a file inside the root directory and hence it is easy to modify without messing up with the partitions.
This tutorial only works with swap file, not the swap partitions.
How do you know if you are using swap file? Try the command below:
cat /proc/swaps
If it shows something like a file under root and the type is file, you have a swap file.
And to remove the swap file, first, you'd have to disable it. This can quickly be done using the given command:
sudo swapoff -a
Then, the swap file can easily be removed using the rm command:
sudo rm /swapfile
But to make sure that the system won't create a new swap file after reboot, you'd have to make a little change in fstab
file.
First, open the fstab file using the following command:
sudo nano /etc/fstab
Now, you'd have to comment out or remove the line associated with the swap file and it would look something like this:
/swap none swap
Once done, save changes and exit from the nano text editor. Now, reboot your Ubuntu machine:
sudo reboot
Once you boot into your system, you can use the given command to check whether the swap file has been removed or not:
sudo swapon --show
And if everything is done correctly, it won't show any output:
Wrapping Up
This was a quick tutorial on removing the swapfile from Ubuntu, by which you can save a couple of gigs if you really need it. It did help me when my server went down, I checked the disk space and it was full. I reduced the swap size in my case though.
I hope you will find this helpful. And if you have any suggestions or queries, let me know in the comments.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.