Linux uses Two types of memory: Random Access Memory (RAM) and swap files. Most computers use RAM to save data and run programs and services because this data isn't saved when you turn your computer off; it is only a temporary allocation. On the other hand, when you use a hard drive, classified as an electro-mechanical data storage device for storing data permanently in your computer, you can save data in your system in a swap file even when it is off.
However, there may be a period when you want to remove the swap file in your Linux system. It could be for various reasons like freeing up space or no longer needed. This article will explain the best methods to delete a swap file in Linux. If you mistakenly deleted a crucial swap file, we’ll also cover how to recover the swap file in Linux; let’s get started.
Part 1. What Is a Swap File?
A swap file is also a system file that aims to create temporary storage space on a hard disk or a solid-state drive (SSD). This usually happens when the computer runs low on memory. The file will swap a section of RAM storage from an idle program to create new space for other programs.
When you use a swap file, your computer can utilize more memory than is physically installed. With the limited space the installed RAM offers, it can now run more programs than it previously could.
Swap files aren’t stored in physical RAM; they’re more of a virtual memory. When you have a swap file, it allows your system to pretend it has more RAM than it does. It will swap the programs and files list used in the RAM to the hard disk until needed. This allowed new programs and fills to be added to the RAM.
Swap files are also known as page files, paging files, or swap space. It is automatically hidden on the system, and its implementation varies with each OS. The OS creates and utilizes a swap file accordingly; it also releases the drive space when it’s no longer necessary by a program.
Part 2. Is It Safe to Delete a Swap File?
Deleting a swap file in Linux is generally safe because it is a temporary file used for virtual memory. You can create it whenever you need it. However, you must remember some crucial factors before you delete a swap file.
The swap file serves as a temporary storage space for data currently not in use so that there is more memory for other tasks. Below are the crucial factors to consider when deleting swap files.
- Enough RAM Space
Before you delete a swap file, ensure you have enough space on your RAM to handle all the applications you’re running. If you delete the swap file without enough RAM space to cover it up, your applications could begin to slow down or crash. The swap file extends the amount of available memory on a system, so ensure your RAM can handle the application’s memory requirements before deleting the swap file.
- Consider Its Performance Impact
Another factor you need to consider is the performance impact of deleting the swap file. Once the swap file is gone, it could significantly impact your system’s performance. Chances are that your PC would become less responsive, especially if you’re running high-demand programs.
- Save All Data
You’d also have to ensure there are no unsaved data in memory. Ensure you save all data before deleting the swap file because this would mean the data is lost once the swap file is gone.
- Stop Processes Using the Swap File
Before you delete the swap file, it’s essential to stop any process actively using the swap file. A
swapon command is a great option to check the swap files currently in use. If a swap file is in use, turn the swapoff command off before deleting it to free up memory.
Part 3. How to Delete a Swap File?
Now that you know what a swap file is. Unlike the swap partition, it’s simply a file inside a root directory, and therefore, it’s easy to modify the swap file without messing up the partition. Before you can delete a swap file in Linux, you must become a superuser. Below is the syntax to remove the swap file in Linux.
# /usr/sbin/swap -d /path/filename
It will remove the swap file name, so you can no longer use it. The file itself will not be deleted. However, run the command below if you want to remove the file space to use it for something else.
Run the following command line to verify that the swap file is unavailable.
# swap -l
If you want to learn if you’re using a swap file, then the command below is what you need.
If it shows a file under root and the type is file, you have a swap file. Additionally, ensure the system won't create a new swap file after reboot. This means you have to make some changes to the fstab file.
First, you have to open the fstab file with the command below.
sudo nano /etc/fstab
After it opens up, comment out or remove the line associated with the swap. It would usually look something like this:
/swapfile none swap
Once you’re done, save changes and exit. Then, reboot your Ubuntu system with the command.
After booting your system, visit the terminal and give the command below to check if the swap file has been removed.
sudo swapon –show
If no output appears, the swap file is gone, and you now have only the RAM to rely on.
Part 4. How to Fix the Operation not Permitted Error When Trying to Delete the Swap File
When you want to delete a swap file you no longer need, you might use the
rm command to get it done. However, sometimes this results in an ‘operation not permitted error.’ This error often occurs because you cannot delete the file. Below are some straightforward steps you can take to fix the permission error.
Step 1: Try running the
rm command as a super user. Below is the command you need to run.
sudo rm /path/to/swapfile
Step 2: If you still receive the error after running the
sudo command, you can use the
ls -l command to check permissions on the file.
Step 3: Stop every process using the swap file. If there are any processes actively using the swap file, it could be actively preventing you from deleting it. The
swapon command will help you check if the swap file is still in use. If it is, run the
swapoff command below to turn off the swap file.
sudo swapoff /path/to/swapfile sudo rm /path/to/swapfile
This should work to fix the ‘operation not permitted’ error when you try to delete the swap file in Linux.
Part 5. How To Recover a Deleted Swap File in Linux?
If you mistakenly delete a crucial swap file in Linux, it could affect the performance of your system, especially when running high-demand programs. Luckily, that’s not the end. One effective tool to recover swap files in Linux after deletion is Wondershare Recoverit Linux Recovery.
Recoverit is a top Linux data recovery software highly recommended for beginners and experts alike. It was designed to recover all forms of data loss and will recover your swap file in Linux. This software has many features that make it the best option for your swap file recovery, and some of them include the following:
Wondershare Recoverit – Your Safe and Reliable Data Recovery Software
5,481,435 people have downloaded it.
It helps in 500+ data loss scenarios like accidental deletion, disk formatting, virus attack, OS crash, power outage, etc.
It is compatible with all mainstream Linux distributions like Ubuntu, Debian, Solus, Manjaro, and more.
It recovers deleted and lost documents, emails, videos, and other 1000+ file types safely and effectively.
Recovering your Swap file in Linux with Wondershare Recoverit is easy. You can follow the steps outlined below or watch the video tutorial.
Step 1: Download and install Wondershare Recoverit on your PC. Then, launch the software and select NAS and Linux > Linux Recovery from the screen.
Step 2: You must connect your Linux computer to the program first. Simply enter the required information and click Connect to create a remote connection between the Linux file recovery software and the Linux device.
Step 3: Recoverit will begin an automatic scan once there’s a connection. Recoverit allows you to filter out the results based on file type, date modified, and many more. You can also stop the scan once you find the file you want to restore.
Step 4: You can now preview the files to ensure they’re what you want. Once you confirm it is the swap file you’re looking for, click Recover and choose a safe location to store your recovered data.
For Windows XP/Vista/7/8/10/11
For macOS X 10.10 - macOS 13
For Windows XP/Vista/7/8/10/11
For macOS X 10.10 - macOS 13
That’s all you have to do; now you have your swap file back, and you can use it to free up space on your system again.
Conclusively, we've covered how to remove a swap file from Linux without causing damage to the system or disrupting the performance of programs. It's best only to delete swap files when they’re no longer required. However, if you mistakenly remove an important swap file, use Wondershare Recoverit to recover the swap file quickly.