I cannot remove my swapfile

rm: /System/Volumes/VM/swapfile0: error 1 Operation not permitted


Posted on Nov 19, 2023 7:41 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 19, 2023 8:07 AM

You cannot delete it.


If you were to successfully delete it while the system is running, your system would most likely panic, or worse start corrupting your file system.


On reboot the old swapfile is removed and a new started.


It is essential to the macOS virtual memory operation.

13 replies

Nov 19, 2023 9:26 AM in response to mbailetti

mbailetti wrote:

ok. thanks for your answer.
anyway I think that when my Mac is running there will be only one copy of this kind of file in the folder VM,
not swapfile0,swapfile1,swapfile2 and so on...
right ?

macOS creates the swapfile to a size, then creates another till it gets too big, then another, etc…


When you reboot, macOS will delete the previous swapfiles and start all over again.


If you have lots of swapfiles, then you are running 1 or more concurrent apps that need lots more RAM memory than is installed in your Mac.


If you have the free disk space, then ignore it.


If you are running out of free disk space, then find something else to delete, because messing with the swapfiles is dangerous for the health of your system.

Nov 19, 2023 12:24 PM in response to VikingOSX

VikingOSX wrote:

The macOS swap file is on a read-only System volume where you have no privileges to add, change, or remove content. Since all UNIX operating systems use swapfiles, and macOS is among them, removing a swap file (if you could) would likely crash the System as some things are locked in the Swap file that are frequently accessed by the operating system.

No. Swapfiles need to be writable. They cannot be on a readonly volume.


But macOS can protected users and utilities that should know better, from attempting to delete the writable swapfiles.

Nov 19, 2023 10:39 AM in response to mbailetti

The macOS swap file is on a read-only System volume where you have no privileges to add, change, or remove content. Since all UNIX operating systems use swapfiles, and macOS is among them, removing a swap file (if you could) would likely crash the System as some things are locked in the Swap file that are frequently accessed by the operating system.

Nov 19, 2023 2:39 PM in response to VikingOSX

/System/Volumes/VM is mounted read/write

mount | grep VM

/dev/disk3s6 on /System/Volumes/VM (apfs, local, noexec, journaled, noatime, nobrowse)

It is its own read/write volume.


vs the macOS boot volume which is mounted read-only

mount | grep read-only

/dev/disk3s1s1 on / (apfs, sealed, local, read-only, journaled)


/System/Volumes/VM is a volume carved out of the APFS container "Apple_APFS Container disk3". This is very similar to the concept used by the DIGITAL Equipment Corporation UNIX AdvFS file system (which was one of the file systems I've worked on, and APFS very much reminds me of AdvFS). In this case APFS has a common pool of storage (the container) and mounted volumes are created and populated using storage from the container. Every mountable volume in the container shares the same free space, which is why if you look at the Available space for several of the volumes in the container, they will all have the same amount of free space:

Filesystem     1024-blocks      Used  Available Capacity Mounted on
/dev/disk3s1s1  1948455240   8938372 1143202848     1%   /
devfs                  346       346          0   100%   /dev
/dev/disk3s6    1948455240        20 1143202848     1%   /System/Volumes/VM
/dev/disk3s2    1948455240   4928756 1143202848     1%   /System/Volumes/Preboot
/dev/disk3s4    1948455240     34628 1143202848     1%   /System/Volumes/Update
/dev/disk1s2        512000      6164     492152     2%   /System/Volumes/xarts
/dev/disk1s1        512000      6228     492152     2%   /System/Volumes/iSCPreboot
/dev/disk1s3        512000      2596     492152     1%   /System/Volumes/Hardware
/dev/disk3s5    1948455240 790281924 1143202848    41%   /System/Volumes/Data

In this example, the following volumes all have 1,948,455,240 Kilobytes of free space (about 1.8 terabytes):

  • / (read-only macOS boot volume)
  • /System/Volumes/VM (where swapfiles live)
  • /System/Volumes/Preboot
  • /Systems/Volumes/Update
  • /System/Volumes/Data (where user and operating system modified files live)


While technically the kernel can do whatever it wants, it is much easier to just use existing VFS (abstracted Virtual File System) APIs to page data into and out of memory, and those VFS APIs pass through to the APFS file system drivers and pay attention to how a volume is mounted. So read-only is really read-only.

Nov 19, 2023 8:13 AM in response to mbailetti

mbailetti wrote:

hi,
i was trying to delete this file as offered by the application "ONYX"


All Volumes share space within the same Container at no penalty.


MacBook-Pro ~ % diskutil list internal

/dev/disk0 (internal, physical):

#: TYPE NAME SIZE IDENTIFIER

0: GUID_partition_scheme *1.0 TB disk0

1: EFI EFI 314.6 MB disk0s1

2: Apple_APFS Container disk1 1.0 TB disk0s2


/dev/disk1 (synthesized):

#: TYPE NAME SIZE IDENTIFIER

0: APFS Container Scheme - +1.0 TB disk1

Physical Store disk0s2

1: APFS Volume Macintosh HD 9.9 GB disk1s1

2: APFS Snapshot com.apple.os.update-... 9.9 GB disk1s1s1

3: APFS Volume Macintosh HD - Data 445.7 GB disk1s2

4: APFS Volume Preboot 2.1 GB disk1s3

5: APFS Volume Recovery 1.2 GB disk1s4

6: APFS Volume VM 2.1 GB disk1s5


MacBook-Pro ~ %



I would not use Onyx.

Nov 20, 2023 7:45 AM in response to BobHarris

Correction. Free space example above is 1,143,202,848 kilobytes or around 1.064 terabytes (using power of 2 based kilobytes and terabytes 🤪 )


And when I said I worked on DIGITAL UNIX AdvFS, I mean I was one of the software developers that helped write and maintain the AdvFS file system (my day job is developing and maintaining Unix based file systems).

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

I cannot remove my swapfile

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.