How to Delete Swap Space in Linux

Recommendation: Read first: How to add swap space in Linux before proceeding with the steps below.

  • View active swap

    Displays currently active swap files and partitions

    swapon --show

  • Deactivate swap

    Disables the swap file so Linux stops using it

    swapoff /docker_swap

  • Remove the entry from /etc/fstab (if it exists)
    vi /etc/fstab
    • Delete the /docker_swap entry
    • Prevents Linux from trying to activate the deleted swap file during boot

  • Delete the swap file

    Removes the swap file from the filesystem

    rm /docker_swap

  • Verify

    Removes the swap file from the filesystem

    free
    swapon --show
    • free -h : checks current RAM and swap usage
    • swapon –show : confirms /docker_swap is no longer active

    Important: Make sure you deactivate the swap before deleting the file. If the system is heavily using swap, swapoff may require enough available RAM to move the swapped page back into memory.