Removing the sleepimage file in /var/vm in macOS permanently
I run high sierra, and the large private/var/vm/sleepimage
file has been bothering me. My computer has 16 GB of ram, which means sleepimage
takes up 16 GB on my drive. I have read tutorials on how to remove it, but it does not work for me. I tried typing sudo rm -R private/var/vm/sleepimage
in the terminal, but the terminal responds rm: private/var/vm/sleepimage: No such file or directory
. What do I do?
Solution 1:
That file can be deleted - no need for -R but be sure to have the full path:
sudo rm /private/var/vm/sleepimage
One you "delete it", the space will not be freed until the kernel and other processes close the file, so restart your Mac. The image file will be recreated next time you need to sleep - so if you had a HDD with that file contiguously allocated, you will make things worse when it's allocated non-contiguous blocks. SSD won't care either way.
I would focus on cleaning / relocating other files long term than this one.
Solution 2:
Info in this answer has been extracted from this page.
The sleepimage file is protected by SIP, which needs to be disabled before deletion. Additionally, you need to turn off the mechanism which creates the sleepimage file to keep it from creating the file in the future.
Turn off hibernate mode (which may be reset during future system upgrades/updates:)
sudo pmset -a hibernatemode 0
Restart your machine and hold down Cmd-R to boot into recovery mode. When that loads, go to Utilities > Terminal, and enter the following to disable SIP:
csrutil disable
reboot
When your machine starts back up, go into terminal and remove the sleepimage. This time, it’ll work. You’ll also need to create a blank sleepimage file and make it unwritable, otherwise the full file will come back fairly quickly.
sudo rm -f /private/var/vm/sleepimage
sudo touch /private/var/vm/sleepimage
sudo chflags uchg /private/var/vm/sleepimage
Now boot back into recovery mode and re-enable SIP:
<s>csrutil enable</s>
reboot
When you’re back into MacOS, go into terminal one final time, and verify that the sleepimage has a size of 0 bytes:
ls -la /private/var/vm