Freeing up disk space on a Linux system often means safely removing the swap file. If you’re wondering how to delete swap file in linux, you’ve come to the right place. This guide walks you through every step, from checking your current swap usage to permanently removing the file without crashing your system.
Swap files act as virtual memory when your RAM runs low. But sometimes you need that disk space back—maybe you’re resizing partitions, moving to a swap partition, or just cleaning up. Whatever the reason, deleting a swap file is straightforward if you follow the right order.
Understanding Swap Files In Linux
Before we jump into deletion, let’s quickly understand what a swap file is. It’s a regular file on your disk that the kernel uses as extra memory. Unlike a swap partition, it’s easy to resize or remove without repartitioning.
Common swap file locations include /swapfile, /swap.img, or custom paths. You can check yours with swapon --show or cat /proc/swaps.
Why You Might Want To Delete A Swap File
- You need to free up disk space urgently
- You’re switching to a swap partition or zram
- You have plenty of RAM and don’t need swap
- You’re resizing the swap file to a different size
- You’re cleaning up after a failed installation
Risks Of Deleting Swap Incorrectly
Deleting a swap file while it’s active can cause system instability or crashes. The kernel might try to write to a file that no longer exists. Always deactivate swap first, then remove the file.
Also, if you delete the swap file without updating /etc/fstab, your system might fail to boot or show errors at startup. We’ll cover that too.
How To Delete Swap File In Linux
Here’s the complete, step-by-step process. Follow each step carefully.
Step 1: Check Current Swap Usage
First, see what swap is active and how much space it uses. Open a terminal and run:
swapon --show
This shows the swap file path, type, size, and usage. For example:
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
You can also use free -h to see total swap and RAM usage.
Step 2: Turn Off The Swap File
You must deactivate the swap file before deleting it. Use the swapoff command with the exact path:
sudo swapoff /swapfile
Replace /swapfile with your actual swap file path. If you have multiple swap files or partitions, turn off only the one you want to remove.
Verify it’s disabled with swapon --show again. It should show nothing for that file.
Step 3: Delete The Swap File
Now you can safely remove the file. Use rm with sudo:
sudo rm /swapfile
This permanently deletes the file. Double-check it’s gone with ls -l /swapfile or ls -l / | grep swap.
Step 4: Remove The Swap Entry From /Etc/fstab
This step is critical. If your system is configured to mount the swap file at boot via /etc/fstab, you must remove that line. Otherwise, the system will try to activate a non-existent swap file and may hang or fail to boot.
Open /etc/fstab in a text editor with sudo:
sudo nano /etc/fstab
Look for a line containing your swap file path. It typically looks like:
/swapfile none swap sw 0 0
Delete that entire line. Save the file (Ctrl+O, then Ctrl+X in nano) and exit.
If you’re using a swap partition instead of a file, the line will have a UUID or device path like /dev/sda2. Only remove the line for the file you deleted.
Step 5: Verify The Changes
Reboot your system to ensure everything works correctly:
sudo reboot
After reboot, check that no swap is active (unless you have other swap sources):
swapon --show
free -h
If you see no swap listed and your system boots fine, you’ve successfully deleted the swap file.
Alternative Method: Using A One-Liner
If you’re comfortable with commands, you can combine steps 2 and 3 into a single line:
sudo swapoff /swapfile && sudo rm /swapfile
Then still edit /etc/fstab separately. Don’t skip that part.
What If You Get “Device Or Resource Busy”?
Sometimes swapoff fails with “device or resource busy.” This means something is actively using swap. You have a few options:
- Close memory-hungry applications and try again
- Use
sudo swapoff -ato turn off all swap at once - Check if you have enough free RAM to handle the swap usage
If you still can’t deactivate it, you may need to boot into recovery mode or use a live USB to remove the file.
Creating A New Swap File (Optional)
If you deleted the swap file but want to create a new one with different size, here’s how:
- Create an empty file:
sudo fallocate -l 2G /newswapfile - Set correct permissions:
sudo chmod 600 /newswapfile - Format as swap:
sudo mkswap /newswapfile - Activate it:
sudo swapon /newswapfile - Add to
/etc/fstab:/newswapfile none swap sw 0 0
Adjust the size (2G) to your needs.
Common Mistakes And How To Avoid Them
Mistake 1: Forgetting To Edit /Etc/fstab
This is the most common error. You delete the file but leave the fstab entry. Next boot, the system tries to activate a missing swap file and may drop you into emergency mode. Always check fstab.
Mistake 2: Deleting The File While Swap Is Active
This can cause kernel panics or data corruption. Always run swapoff first.
Mistake 3: Using Wrong Path
Double-check the exact path with swapon --show. Deleting the wrong file could break your system.
Mistake 4: Not Checking Free RAM
If you have very little RAM and swap is heavily used, turning it off might cause out-of-memory errors. Check free -h and ensure you have enough free RAM.
When Should You NOT Delete Swap?
Consider keeping swap if:
- You have less than 4GB of RAM
- You run memory-intensive applications (VMs, databases, compilers)
- Your system uses hibernation (swap must be at least as large as RAM)
- You’re on a server with unpredictable workloads
Deleting swap without enough RAM can cause the OOM killer to terminate processes randomly.
Checking Disk Space After Deletion
After removing the swap file, verify the freed space:
df -h
You should see the available space increase by the size of the deleted swap file. For example, if you had a 2GB swap file, your root partition should show about 2GB more free space.
Automating Swap File Removal With A Script
If you manage multiple systems, you can automate the process with a bash script:
#!/bin/bash
SWAPFILE="/swapfile"
sudo swapoff $SWAPFILE
sudo rm $SWAPFILE
sudo sed -i '/swapfile/d' /etc/fstab
echo "Swap file $SWAPFILE removed successfully."
Make it executable with chmod +x remove_swap.sh and run with sudo. Adjust the path as needed.
Troubleshooting Boot Issues After Deletion
If you forgot to edit fstab and your system fails to boot, here’s how to fix it:
- Boot into recovery mode or use a live USB
- Mount your root partition:
sudo mount /dev/sda1 /mnt(adjust device) - Edit fstab:
sudo nano /mnt/etc/fstab - Remove the swap line and save
- Reboot
This should resolve the boot issue.
Swap File Vs Swap Partition: Which Is Better?
Swap files are more flexible—you can resize or delete them without repartitioning. Swap partitions are slightly faster but harder to modify. For most modern systems, a swap file is perfectly fine.
If you’re deleting a swap file to switch to a swap partition, the process is similar: deactivate the file, remove it, update fstab, then create the partition and add it to fstab.
Performance Considerations
Deleting swap doesn’t improve performance unless you were using swap heavily. In fact, if your system runs out of RAM, it will crash or kill processes. Only delete swap if you have sufficient RAM for your workload.
Some users disable swap to extend SSD life, but modern SSDs handle swap writes well. The performance gain is minimal.
Frequently Asked Questions
Can I Delete Swap File While System Is Running?
Yes, but you must deactivate it first with swapoff. Never delete an active swap file.
What Happens If I Delete Swap File Without Swapoff?
The kernel may panic or crash. Data in swap could be lost, and the system might become unstable.
How Do I Find Where My Swap File Is Located?
Run swapon --show or cat /proc/swaps. Both show the exact path.
Is It Safe To Delete Swap File On A Server?
Only if you have enough RAM for all services. Servers often benefit from swap to handle traffic spikes.
Can I Delete Swap File Without Editing Fstab?
Technically yes, but the next boot will fail or show errors. Always remove the fstab entry.
Final Thoughts
Deleting a swap file in Linux is a simple process when you follow the correct steps: deactivate, remove, update fstab. The key is to never skip the fstab edit. With this guide, you now know exactly how to delete swap file in linux safely and efficiently.
Remember to always check your RAM usage first. If you’re unsure, keep a small swap file—it’s better to have it and not need it than to need it and not have it.
Freeing up that disk space can be satisfying, but make sure your system remains stable afterward. Happy Linux administrating!