October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
VGSources
Blog

How to Delete a Swap File on Linux Safely

A safe, distribution-aware procedure for removing a Linux swap file, including memory checks, fstab cleanup, systemd reloads, verification, troubleshooting, and differences between files, partitions, LVM, zram, and managed swap.
Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To delete a Linux swap file safely, first confirm that the file is the swap area in use, disable it with sudo swapoff -v /swapfile, remove or comment out its line in /etc/fstab, reload systemd when appropriate, and only then delete it with sudo rm /swapfile. Finish by checking swapon --show, and reboot if you want to prove the file is not enabled again.

What you are actually removing

A swap file is a regular file whose disk blocks Linux uses as overflow memory. When RAM pressure rises, the kernel can move less-active memory pages into that area, leaving RAM available for other work. A swap file is different from a swap partition, an LVM logical volume, and compressed swap in RAM (zram). The command sequence in this guide applies to a file such as /swapfile; it must not be used as a substitute for removing a partition or logical volume.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Deleting a file that is still active is the central error to avoid. The kernel can continue referring to the file’s disk blocks while swap is enabled. Turn the area off first, remove the boot-time configuration second, and erase the file last.

Check which swap area is active

Do not assume that /swapfile is the target. A machine may have several swap areas or may be using a completely different type.

  1. List active swap devices and files:
    swapon --show

    The output identifies the active path, type, size, amount in use, and priority. A row whose type is file and whose name is /swapfile is the usual target for this procedure.

  2. If you need a second view, read the kernel’s current list:
    cat /proc/swaps
  3. Check total memory and swap consumption:
    free -h

    Look at the Swap row, especially the used value. A high used value means that disabling swap will require those pages to be brought back into RAM.

If the listed target is a partition, an LVM path, or a zram device, stop here and use the corresponding section below. Running rm against a device path is not how those resources are removed.

Identify the boot-time entry before changing anything

Most traditional Linux installations enable a swap file at boot through /etc/fstab. Make a backup before editing it:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo cp /etc/fstab /etc/fstab.backup

Locate an entry that names the file:

grep -nE '(^|[[:space:]])/swapfile([[:space:]]|$)' /etc/fstab

A common line is:

/swapfile none swap defaults 0 0

The path in your output must match the path shown by swapon --show. If the active file has another name or location, substitute that exact path in every command. If the search returns no line, inspect the complete file for an equivalent entry, generated configuration, or a distribution-specific service before deleting anything. A missing line means that removing a line is not part of this particular cleanup, but it does not make it safe to delete an active file.

Prepare enough RAM for swapoff

swapoff must move swapped pages back into physical memory. If RAM cannot hold them, the command may fail rather than completing safely. Before disabling a heavily used swap file:

  • Save work and close memory-heavy graphical applications, browsers with many tabs, compilers, and other large processes.
  • Stop containers, virtual machines, or batch jobs that can release substantial memory. Use the management command appropriate to your workload rather than killing processes blindly.
  • Check free -h again after closing those workloads. The exact amount of free memory required depends on the pages currently in swap and the applications running.
  • Use a maintenance window when the host provides an important service. Disabling swap can increase pressure on RAM even when it succeeds.

Do not treat the swap file’s nominal size as proof that the same amount of additional RAM is needed; only the currently used portion has to be brought back. Nevertheless, leave headroom for normal activity instead of attempting the operation while the machine is already near its memory limit.

Delete a standard swap file step by step

Run these steps as an administrator. The examples use /swapfile; replace it with the verified path when necessary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

1. Disable the active file

sudo swapoff -v /swapfile

The verbose option shows the target being processed. A successful command returns to the shell without an error. Confirm immediately:

swapon --show
cat /proc/swaps

The target file should no longer appear. If it remains listed, do not continue to rm; investigate why it is still active.

2. Remove or comment out its fstab line

Open the file with an editor you are comfortable using, for example:

sudoedit /etc/fstab

Delete the line for the swap file, or place a # at its beginning to comment it out. Preserve every unrelated mount and swap entry. Save the file and inspect the result:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
grep -nE '(^|[[:space:]])/swapfile([[:space:]]|$)' /etc/fstab

It should produce no active (uncommented) entry for the removed path. Removing this configuration matters because a future boot may otherwise try to activate a file that no longer exists.

3. Reload systemd when fstab units may have been generated

sudo systemctl daemon-reload

This reload is conditional rather than universally required. On systems where systemd generated mount or related units from /etc/fstab, it makes the manager reread the changed configuration. Running it after the edit is harmless on a systemd host and avoids leaving a stale generated view in memory.

4. Delete the now-inactive file

sudo rm /swapfile

Use the exact path you verified. The command permanently removes the file, so confirm that it is not a valuable ordinary file accidentally given a similar name. If you prefer an additional check, run ls -l /swapfile before deletion and verify its ownership, size, and location.

5. Verify the running system

swapon --show
free -h

swapon --show should no longer list the deleted file. The Swap row from free -h should show no active capacity unless another swap area remains. A machine can legitimately show another file, partition, LVM volume, or zram device; verify that each remaining entry is intentional.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

6. Optionally verify after a reboot

Reboot only when the machine can tolerate downtime. After it returns, run swapon --show and check that the deleted path was not re-enabled. This catches an overlooked fstab line or another boot-time service that recreates or activates swap.

Editing fstab without creating a boot problem

/etc/fstab controls more than swap. A typo in an unrelated filesystem line can delay or interrupt boot, which is why the backup is important. Keep the backup until you have rebooted successfully.

  • Change only the line that names the target swap file.
  • Do not remove a neighboring mount entry merely because it appears on the next line.
  • Commenting the line is reversible; deleting it is cleaner once you are certain the swap file will not be reused.
  • If you discover that the line points to a different path than the active swap output, correct the path diagnosis before making edits.

If a mistake prevents normal startup, use a recovery environment or emergency shell to restore /etc/fstab from /etc/fstab.backup. The exact recovery menu and mount commands vary by distribution, so follow that distribution’s recovery procedure rather than guessing at a repair command.

Troubleshoot common failures

swapoff reports that it cannot allocate memory

This normally indicates that the pages currently in swap cannot all fit back into available RAM. Close or stop more workloads, wait for memory use to fall, and retry. If the host cannot operate without the swap area, do not force deletion; keep it enabled and plan a controlled change with additional memory or another swap area available.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The file still appears after swapoff

Check the exact path and spelling in swapon --show. You may have disabled one swap area while another entry remains active. If the same path is still listed, rerun the command with that exact path and inspect errors. Do not remove the file until the listing is clear.

swapoff says the target is not active

The file may already be disabled, or the path may not be the one currently in use. Confirm with swapon --show and cat /proc/swaps. If neither lists it, you can proceed with fstab cleanup and file deletion after confirming that the path is correct.

The system tries to activate the missing file at boot

An uncommented fstab line is the first thing to check. Search again after editing, then run sudo systemctl daemon-reload on a systemd host and reboot when convenient. If no fstab line exists, look for a distribution-specific swap-management service that creates or enables the file.

The file cannot be removed

Confirm that it is inactive, that you are using the correct path, and that your account has administrator privileges. A permission error is normally fixed by using sudo rm; a path error is fixed by returning to the output of swapon --show, not by deleting similarly named files.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When the active swap is not a file

Swap partition

A partition appears as a device path rather than a regular file. Disable that device with swapoff, remove or comment out its fstab entry, and then use a partition-management method appropriate to your system if you intend to reclaim the space. There is no file to delete with rm. Removing the partition itself is a separate, potentially destructive storage operation.

LVM logical volume

An LVM swap area is also a device, commonly represented by an LVM path. First disable the device with swapoff and remove its fstab entry. Only then should you remove the logical volume with your normal LVM administration tools. Confirm the volume name carefully; deleting the wrong logical volume can destroy unrelated data.

zram

zram provides compressed swap in RAM and is not a disk file. Do not search for a file to remove. Identify the service or configuration that creates the zram swap device, disable it using the distribution’s documented mechanism, and verify with swapon --show. The exact service names and configuration files differ, so inspect the active unit and your distribution’s documentation before changing them.

dphys-swapfile-managed systems

Some Raspberry Pi OS and similar installations manage a swap file through dphys-swapfile. In that arrangement, the tool may recreate or resize the file. Follow the tool’s own disable or removal procedure instead of deleting the generated file by hand, then verify the result with swapon --show. Removing an fstab line alone may not stop a manager from creating swap again.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decide whether removing swap is appropriate

Successful deletion does not prove that removing swap was the best operational choice. Swap can provide a buffer during temporary memory spikes, while a system with ample RAM and a deliberate memory policy may not need a disk-backed file. Before removal, consider:

  • Whether the host runs workloads with unpredictable memory peaks.
  • Whether containers or virtual machines can exhaust RAM during normal operation.
  • Whether you are reclaiming storage for a specific purpose and have another swap area available.
  • Whether hibernation or another workflow depends on swap capacity; the supplied command sequence does not establish requirements for those features.
  • Whether the machine is a server that must remain available while pages are moved back into RAM.

If you only need to replace or resize the file, create and enable a replacement swap area before disabling the old one, verify that the replacement is active, and then remove the old entry and file. That approach avoids leaving the host without any swap during the transition, but the replacement’s size and creation procedure depend on your distribution and storage layout.

Final verification checklist

  • Target confirmed: swapon --show identified the exact file path.
  • Memory checked: free -h showed that the host could absorb pages currently in swap, with heavy workloads closed when necessary.
  • Swap disabled: sudo swapoff -v /swapfile completed and the file disappeared from swapon --show and /proc/swaps.
  • Boot entry removed: the matching /etc/fstab line was deleted or commented out, with a backup retained.
  • Configuration reloaded: sudo systemctl daemon-reload was run where systemd-generated configuration could be involved.
  • File deleted: sudo rm /swapfile was run only after deactivation.
  • Persistence checked: a post-reboot swapon --show confirmed that the removed path did not return.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.