How to Disable Swap on Raspberry Pi OS and Use a USB Drive Instead

How to Disable Swap on Raspberry Pi OS and Use a USB Drive Instead


This tutorial is focused on how to disable the default swap space on the SD card of your Raspberry Pi and optionally set up a swap space on an external USB drive. Using swap on an SD card can significantly reduce its lifespan due to the high write load. An external USB drive, while also susceptible to wear from swap usage, is easier and cheaper to replace.

Disabling Swap on the SD Card

To prevent your Raspberry Pi's SD card from being used as swap space, follow these steps:

  1. Turn off the swap file: Open a terminal and execute the following command to stop the swap:

    sudo dphys-swapfile swapoff
  2. Uninstall the swap file: Remove the swap file from your system:

    sudo dphys-swapfile uninstall
  3. Remove the swap file service: To ensure the swap file service does not start on boot, run:

    sudo update-rc.d dphys-swapfile remove
  4. Optionally, remove the swap file manager: If you're sure you won't need to re-enable swap on the SD card in the future, you can remove the swap file manager entirely:

    sudo apt purge dphys-swapfile -y
  5. Verify the swap is disabled: Reboot your Raspberry Pi. After rebooting, check that swap is indeed disabled by running:

    free -m

    The output should show 0 in the swap row, indicating no swap space is in use.

Setting Up Swap on an External USB Drive

If your Raspberry Pi needs swap space to function properly, follow these steps to set up a swap partition on an external USB drive:

  1. Prepare the USB drive:

    • Connect your USB drive to the Raspberry Pi.
    • Identify the device name of your USB drive by running lsblk. It's often named /dev/sda or similar.
    • Use cfdisk to create a new partition table and a primary Linux swap partition on the USB drive:
      sudo cfdisk /dev/sda
      • Select gpt when prompted for a partition table type.
      • Create a new partition, select it to be a Linux swap partition, write the changes, and quit.
  2. Format the partition as swap:

    • Format the new partition as swap, forcefully, to ensure it's clean:
      sudo mkswap -f /dev/sda1
    • Note the UUID provided by the output.
  3. Update /etc/fstab:

    • Edit the /etc/fstab file to add the new swap partition. Replace UUID with the one noted earlier:
      UUID=your-uuid-here swap swap defaults 0 0
  4. Enable the swap partition:

    • You can enable the swap immediately without rebooting by running:
      sudo swapon /dev/sda1
    • Verify it's working by checking the swap usage:
      free -mh
  5. Disable the SD card-based swap service (if not already done): If you've followed the initial steps to disable the SD card swap, this should already be done. If not, execute the commands listed in the "Disabling Swap on the SD Card" section again.


By following these steps, you've successfully disabled swap on your Raspberry Pi's SD card, potentially prolonging its life. Additionally, if needed, you've set up a swap space on an external USB drive, which can be easily replaced if worn out. This setup enhances the reliability of your Raspberry Pi for long-term projects.