Debian – How to Safely Turn Off Swap Permanently and Reclaim Space

debianfilesystemslinuxpartitionswap

I installed Debian Jessie with default partitioning on my SSD drive. My current disk partitioning looks like this:

My current disk partitioning looks like this

As I have 16GB of RAM, I assume I don't need swap. But since I have other disk drives I may create a swapfile for example, on one of the other drives instead.

Can you tell me what steps I should take to remove the swap partition correctly and permanently for it not to occupy disk space? I wish to delete the swap partition as I currently have only 128GB SSD.

Here is what I tried and rebooted each time; each of these steps being not permanent, or did not do anything:

  1. Using the swapoff utility:

    swapoff --all
    
  2. Using the GParted utility:

    Right-clicking the swap partition and clicking Swapoff.

  3. Commenting out the swap partition's UUID in the following file:

    /etc/fstab
    
  4. Commenting out the swap partition's UUID in the following file:

    /etc/initramfs-tools/conf.d/resume
    
  5. Running these commands in the end (both in this and the opposite order):

    update-grub
    
    update-initramfs -u
    

Best Answer

  1. If you have GParted open, close it. Its Swapoff feature does not appear to to be permanent.

  2. Open terminal and become root (su); if you have sudo enabled, you may also do for example sudo -i; see man sudo for all options):

     sudo -i
    
  3. Turn off the particular swap partition and / or all of the swaps:

     swapoff --all
    
  4. Make 100% sure the particular swap partition partition is off:

     cat /proc/swaps
    
  5. Open a text editor you are skilled in with this file, e.g. nano if unsure:

     nano /etc/fstab
    
  6. Comment out / remove the swap partition's UUID, e.g.:

     # UUID=1d3c29bb-d730-4ad0-a659-45b25f60c37d    none    swap    sw    0    0
    
  7. Open a text editor you are skilled in with this file, e.g. nano if unsure:

     nano /etc/initramfs-tools/conf.d/resume
    
  8. Comment out / remove the previously identified swap partition's UUID, e.g.:

     # RESUME=UUID=1d3c29bb-d730-4ad0-a659-45b25f60c37d
    
  9. Don't close the terminal as you will need it later anyway.

Note: The next steps differ depending on, whether you rely on CLI or GUI.


GUI:

  1. Open up GParted, either from menu, or more conveniently from the terminal we have opened:

     gparted
    
  2. If you don't have it installed, you may do so; afterwards run the previous command again:

     apt-get install gparted
    
  3. Choose your drive from top-right menu.

  4. As the GParted reactivates the swap partition upon launch, you will have to right-click the particular swap partition and click Swapoff -> This will be applied immediately.

  5. Delete the swap partition with right click -> Delete. You must apply the change now.

  6. Resize your main / other partition with right click -> Resize/Move. You must apply the change now.

  7. Back to the terminal, let's recreate the boot images:

     update-initramfs -u -k all
    
  8. Update GRUB:

     update-grub
    
  9. You may reboot now if you wish to test that the machine boots up.

Encryption note: If your swap partition is encrypted, then you also need to comment out the related line in /etc/crypttab, otherwise CryptSetup will keep you waiting for 90 seconds during boot time. Thanks frank for this addition.


CLI:

I will check in VMs if my solution works, then I will share it. In the meantime, see this answer.

Related Question