Remove a Swap file from Swap space in Ubuntu

We consider this article to be an extension of – Swap file to extend Swap space in Ubuntu. As we have already covered in that article, there are two types of Swap space available –

  1. Swap partition and,
  2. Swap file.

We can extend Swap space through a Swap file. But, due to reasons like hardware upgrade – we may choose to get rid of Swap file entirely leaving the Swap partition stay as it is. So, it is better to remove Swap file if it isn’t required anymore. Also, it unnecessarily takes up disk space.

We need to follow a process to remove a Swap file. It isn’t as straightforward as removing any other file with rm command-line utility. We cover that in next section.

Note: Following operations require superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance.

Remove a Swap file

Firstly, we need to check for the Swap file enabled on our system. There could be multiple Swap files. So, identify the Swap file we want to remove through –

swapon --show

We continue the example from previous article, let’s say have 1GB of Swap file in – /swap1G.img

So, to disable the Swap file –

sudo swapoff /swap1G.img

Again, run the following code. This time around it wouldn’t list the Swap file we have disabled.

swapon --show

Now, we can delete the Swap file through rm command-line utility –

sudo rm /swap1G.img

Wait, at this stage you may think everything is done. But, what about entries in /etc/fstab. We need to edit that file as well. So, use a text-editor we have used nano

sudo nano /etc/fstab

And, delete the following entries –

/swap1G.img      swap      swap      defaults      0      0

Save and exit.

In conclusion, we have covered how to remove a Swap file from Swap space in Ubuntu here.

Similar Posts