40

I created a swap file in Ubuntu by following this process, but I no longer need it, so I would like to delete it.

However, the blog article doesn't write anything regarding the deletion, so I tried deleting it via sudo rm -rf, but it got the Operation not permitted error.

So far, I tried many answers to how to delete a file with the same error, but nothing worked in my case:

, which include:

  • change the permission of both the swapfile and the root directory / (hmod ugo+w .)
  • change the immutable flag on both the swapfile and / (chattr -i -a .)
  • reboot the system

All of them didn't work. I wonder how I can delete it, but if it is a swap file, how can I delete it?

The result of free -h is:

              total        used        free      shared  buff/cache   available 
Mem:           1.7G        101M        405M        1.2M        1.2G        1.4G
Swap:          1.5G        234M        1.3G
muru
  • 197,895
  • 55
  • 485
  • 740
Blaszard
  • 831
  • 4
  • 13
  • 24
  • Is the swapfile still online? Please enter the command free -h and append the results of that into your question. – Charles Green Oct 26 '17 at 02:30
  • 1
    So, the swap file is still being used by the swap process. Please execute the command sudo swapoff -a and then try to delete the swapfile. – Charles Green Oct 26 '17 at 02:32
  • @CharlesGreen It worked! Thanks. Could you consider adding it as an answer? I'll give you +25 rep. – Blaszard Oct 26 '17 at 02:34

2 Answers2

65

The output of free -h indicates that swap is being used - the swap process is still running.

Enter the command

sudo swapoff /path/to/swapfile/to/be/deleted

This will disable the swapfile, and the file can be deleted at that point.

Please note that if you have created an entry in /etc/fstab for the swapfile, you should also delete it (or comment it out by adding # at the beginning of the line).

vstepaniuk
  • 481
  • 3
  • 15
Charles Green
  • 21,339
  • Thanks. Why is the last step needed? – Blaszard Oct 26 '17 at 02:38
  • Part of the tutorial you were looking at earlier instructed you in how to add the swapfile to your fstab, so that it would be mounted at boot time. I am unsure what would happen if you were to start your computer, and it tried to mount a non-existant file. I assume it would simply generate an error, but I'd rather not test it. – Charles Green Oct 26 '17 at 02:40
  • fyi: the # turns the line into a comment; allowing easy restoration if you later decide you want it, but is ignored by the system... – guiverc Oct 26 '17 at 03:01
  • 4
    Why disable all swapfiles just to remove one? You can just do sudo swapoff /path/to/swapfile/to/be/deleted without removing any other. – Ruslan Oct 26 '17 at 05:42
  • 1
    @Ruslan If I had been smart enough to ask the OP to run swapon --show prior to answering, this would have been a great idea. – Charles Green Oct 26 '17 at 14:57
  • 1
    If the system tries to mount a swap-partition via fstab and the partition does not exist, it will increase boot-time by 90 seconds, this is my experience. System will still boot. I had the problem due to a changed UUID and during boot I could see a timer counting down from 1min 30sec. – mook765 Oct 28 '17 at 08:24
3

Run the following commands to delete the swap file:

sudo swapoff -a -v
sudo rm /swapfile
#back up /etc/fstab:
sudo cp /etc/fstab /etc/fstab.bak
sudo sed -i '/\/swapfile/d' /etc/fstab
khalidh
  • 140
  • Can you please elaborate last line (i.e. sudo sed -i '/\/swapfile/d' /etc/fstab) of suggested commands what does it exactly do? Thank you in advance. – NikolaS Jun 30 '23 at 10:43