Today’s article will be a bit more advanced than some as I try to explain how to delete a swapfile in Linux. If I do my job right, it’ll be easy enough for a newbie to follow. However, I’m not going to suggest newbies mess around with removing swapfiles – but they certainly can.
It should probably be noted that some folks will call this a “swapfile” and others will add a space to call it a “swap file”. I’m not sure which is correct, so I’m sticking with “swapfile” for consistency sake. After all, about two years ago, I wrote an article about how you can enable a swapfile.
How To: Create And Enable A Swapfile
This article might be a bit complicated. The directions themselves are pretty simple. Distilling the concept of ‘swap’ down to an 800-word article is the difficult part.
What Is A Swapfile:
What is swap? Well, it’s not really a place where the kernel sticks stuff when there’s no more free RAM. People seem to think that’s what swap is, and it’s not. Even on systems with lots of RAM, the kernel will use swap when it is available.
It’s quite a bit more complicated than that, though it can sort of be related to that. It would be prudent to suggest that you read this thread first, concentrating on the first post in the thread:
To swap or not to swap, that is the question.
I’d further suggest reading this article from the folks at linux.com:
There’s more to it than the article goes into, but pay attention to this bit of text quoted below:
Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.
In the early years, we used a special partition for our swap. These days, there probably isn’t much need for a special partition. Most of us that still use swap will be using a swapfile. This is essentially the same thing, but it is a file instead of a special partition on your storage drive.
Your distro may have created a swapfile during the installation process. You may have created your swapfile along the way. Well, this article is going to tell you how to delete a swapfile, just in case you changed your mind.
Delete A Swapfile In Linux:
Yeah, this sort of thing pretty much requires using the terminal. I don’t know of a way to do this graphically, though there’s probably a way that I don’t know about. So, press
With your terminal now open, let’s first make sure that you have swap to begin with. To do that, you can run this command:
1 | free -m |
Technically, you don’t need the -m flag, but it’s easier to read in my opinion. The output might look something like this:
1 2 3 4 | $ free -m total used free shared buff/cache available Mem: 15873 6001 1034 753 8837 8790 Swap: 2047 737 1310 |
As you can see, the system has 16 GB of RAM and 2 GB of swapfile space. Of that 2 GB of swapfile, 737 MB is being used. There’s lots of free RAM, but swap is still being used. Like I said, it’s not just a place where the kernel stuffs things when there’s no more free RAM.
So, now that we’ve learned there’s a swapfile, you can run this command:
1 | swapon --show |
The output from that might look something like this:
1 2 3 | $ swapon --show NAME TYPE SIZE USED PRIO /swapfile file 2G 737.6M -2 |
The bit we’re looking for is /swapfile because that’s the literal name of the swapfile. You could have called it anything while setting it up, so it’s important to get the name correct for the next command.
Now, we want to delete your swapfile. That’s a very easy command. It looks like this command:
1 | sudo rm /<swapfile_name> |
In my example, if I wanted to delete a swapfile on my system, the command would look exactly like this:
1 | sudo rm /swapfile |
To confirm that the file is deleted and no longer being used, just run this command all over again:
1 | free -m |
It might seem a little complicated at first and I hope I explained it well. Using swap (or not) can be a complicated subject, but it’s not all that taxing to delete a swapfile in Linux. If you can follow the above directions, you should be good to go.
Closure:
As I’ve mentioned before, I leave swap enabled because the kernel uses it. The kernel uses swap even on systems with a lot of free RAM. I figure the kernel is smarter than I am, so I leave it enabled.
The second link in this article is well worth reading. Deciding to use swap or not is a very personal thing. If you have plenty of RAM, you can get away with not using any swap at all. Of course, if your system is fast enough and you’re using an SSD, having a swap isn’t going to add much latency to your system – if any at all.
This is something you get to decide. You can decide what is right for you and your computing needs. If you want a swapfile, you can easily add one. If you want to delete a swapfile that was created during the installation, you can do that as well. This is Linux and your configuration options are nearly endless. You decide what’s best for you. You decide how you run your Linux because it is your Linux. And that is awesome.
Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.
Last Updated on November 1, 2023 by KGIII
Thanks for the tutorial, I was looking for a way to fix this, thanks a lot