Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsTo 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.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
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.
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 Best Overall
- List active swap devices and files:
swapon --showThe output identifies the active path, type, size, amount in use, and priority. A row whose type is
fileand whose name is/swapfileis the usual target for this procedure. - If you need a second view, read the kernel’s current list:
cat /proc/swaps - Check total memory and swap consumption:
free -hLook at the
Swaprow, 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:
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →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 -hagain 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.
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.
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.
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.
Rank #4
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.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchThe 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.
Best Value
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.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →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.
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.
Quick Recap
Final verification checklist
- Target confirmed:
swapon --showidentified the exact file path. - Memory checked:
free -hshowed that the host could absorb pages currently in swap, with heavy workloads closed when necessary. - Swap disabled:
sudo swapoff -v /swapfilecompleted and the file disappeared fromswapon --showand/proc/swaps. - Boot entry removed: the matching
/etc/fstabline was deleted or commented out, with a backup retained. - Configuration reloaded:
sudo systemctl daemon-reloadwas run where systemd-generated configuration could be involved. - File deleted:
sudo rm /swapfilewas run only after deactivation. - Persistence checked: a post-reboot
swapon --showconfirmed 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.




