Swap space in Linux acts as overflow for physical RAM, providing additional virtual memory when running many processes or memory‑intensive workloads. A swap file created on an existing filesystem avoids repartitioning disks and can be convenient on virtual machines or cloud instances. When that swap file is no longer required, removing it frees disk space and simplifies resource planning.

The kernel’s virtual memory subsystem moves less frequently used pages from RAM into swap space, which may live on a dedicated partition or on a regular file marked and activated as swap. Swap files are typically referenced in /etc/fstab so they are enabled automatically at boot with swapon. Cleaning up a swap file requires deactivating it at runtime and removing any persistent configuration so it is not silently recreated.

Disabling a swap file on a system with limited RAM can increase the risk of out‑of‑memory conditions under heavy load, causing processes to be killed or the system to become unresponsive. Checking current memory and swap usage before proceeding reduces the chance of surprising failures. Safe removal assumes shell access with sudo privileges and applies only to swap files, not to swap partitions, which must be handled with different steps.

Steps to remove a swap file in Linux:

  1. Open a terminal with sudo privileges.
    $ whoami
    user
    $ groups
    user sudo

    Being in the sudo group or an equivalent administrative group allows privileged commands required for swap management.

  2. List active swap areas to identify the swap file path.
    $ swapon --show
    NAME      TYPE  SIZE   USED PRIO
    /swapfile file  2G     0B   -2

    The NAME column lists each active swap device or file and shows whether a swap file such as /swapfile is in use.

  3. Check current memory and swap usage to evaluate whether disabling the swap file is safe.
    $ free -h
                   total        used        free      shared  buff/cache   available
    Mem:            7.7G        2.1G        3.5G        256M        2.1G        5.2G
    Swap:           2.0G          0B        2.0G

    High Swap usage under normal load can indicate that removing the swap file may cause out‑of‑memory conditions and process termination.

  4. Disable the swap file so it is no longer used by the kernel.
    $ sudo swapoff /swapfile

    The command returns no output when the swap file is successfully deactivated.

  5. Confirm that the swap file is no longer reported as active.
    $ swapon --show

    No output indicates that no swap areas are active; if other swap devices remain, ensure the swap file path no longer appears.

  6. Open /etc/fstab in a text editor to edit the swap configuration.
    $ sudo nano /etc/fstab

    Any preferred text editor such as vim or gedit can be used instead of nano.

  7. Remove the swap file entry from /etc/fstab.
    # /etc/fstab: static file system information.
    #
    # <file system> <mount point>   <type>  <options>       <dump>  <pass>
    /swapfile none swap sw 0 0

    Delete the line containing the swap file path so the swap file is not re‑enabled automatically on future boots.

  8. Save the modified /etc/fstab file to persist the configuration change.

    For nano, press Ctrl+O to write the file and Ctrl+X to exit back to the shell.

  9. Remove the swap file from disk.
    $ sudo rm /swapfile

    Confirm the swap file path carefully before running rm; deleting the wrong file can permanently remove unrelated data.

  10. Verify that the swap file is no longer active or configured.
    $ swapon --show
    $ grep swapfile /etc/fstab

    Absence of the swap file path in both commands confirms that the swap file has been fully removed from runtime state and persistent configuration.

Discuss the article:

Comment anonymously. Login not required.