Swap space in Linux provides virtual memory when RAM is full. When RAM runs out, inactive memory pages are moved to the swap space, located on the hard drive, which is slower than RAM. While useful for systems with limited memory, swap is not a replacement for adding more RAM.

At times, you may need to remove an unnecessary swap file to free up disk space. This process must be done carefully to ensure the system isn’t relying on it. Removing the swap file can optimize disk usage without affecting system performance.

Proper steps must be taken to disable the swap, delete its configuration, and remove it from the system. This ensures the swap file won’t be reused or recreated after removal.

Steps to remove a swap file in Linux:

  1. Disable the swap file.
    $ sudo swapoff /path/to/swapfile
  2. Edit the fstab file and remove the swap file entry.
    $ sudo nano /etc/fstab

    Locate and remove the line with the swap file path.

  3. Delete the swap file from the file system.
    $ sudo rm /path/to/swapfile

    Ensure that you've correctly specified the swap file path to avoid deleting unintended files.

  4. Verify that the swap file is no longer in use.
    $ swapon --show

    The swap file should not appear in the output if removed successfully.

Discuss the article:

Comment anonymously. Login not required.