APFS snapshot deletion issue: how to process multiple terminal commands at once?

Hello,

delete APFS snapshots Time Machine JL tmutil.kmmacros (22.4 KB)

I discovered to my dismay how important it is to keep and eye on and manage APFS snapshot files. My computer was crashing repeatedly and I was out of space only to discover that a 495 GB APFS file had been created. After deleting the file, my mac ran smoothly. Interesting: apple's assurances that APFS files are purgeable space is nonsense. I was constantly getting no more free space alerts and could not carry out simple tasks like upgrading my iphone via my mac. I notice that the APFS snapshots are reaccumulating and there is nothing I can do about it.

I am creating a macro to delete APFS files using Terminal commands (which I think / hope is bash script).

  • I list all APFS files: tmutil listlocalsnapshots /

  • I get a result like:

com.apple.TimeMachine.2019-08-09-112438
com.apple.TimeMachine.2019-08-09-122242
com.apple.TimeMachine.2019-08-09-132016
com.apple.TimeMachine.2019-08-09-142431
com.apple.TimeMachine.2019-08-09-152657
com.apple.TimeMachine.2019-08-09-172057
com.apple.TimeMachine.2019-08-09-182009
etc

  • I copy the result to the clipboard, and search and replace
    com.apple.TimeMachine. with sudo tmutil deletelocalsnapshots and end up with

sudo tmutil deletelocalsnapshots 2019-08-10-112348
etc similar for each line
sudo tmutil deletelocalsnapshots 2019-08-09-112438
sudo tmutil deletelocalsnapshots 2019-08-09-122242
sudo tmutil deletelocalsnapshots 2019-08-09-132016
sudo tmutil deletelocalsnapshots 2019-08-09-142431
sudo tmutil deletelocalsnapshots 2019-08-09-152657
sudo tmutil deletelocalsnapshots 2019-08-09-172057
sudo tmutil deletelocalsnapshots 2019-08-09-182009
etc

I now want to batch delete all snapshots in Terminal mode in my macro.
I add an action which creates a batch script which should carry out all of these but it does not work. My bash syntax is obviously wrong

image

Probably this snapshot was not created as a 495GB snapshot but it grew in size over the time. (As long as one snapshots exists, every file you delete – and which was present at the time of the snapshot – will not be deleted; it will “go” into that snapshot instead, and the snapshot will grow endlessly, like a never emptied Trash can.)

I once had a similar problem with an enormous snapshot that turned out to be a silent leftover of a malfunction of ChronoSync three months before.


I guess, this depends on how your read that “assurance”:

It is certainly purgeable in the sense that when you delete a snapshot you will not lose any of your current data.

The other question is, whether snapshots are automatically purged by the system. This might also depend on how the snapshot has been created:

  • If it was created by the system (e.g. Time Machine), I would also expect that it gets automatically purged, when running low on space for example.
  • If created by a third party app (CCC for example), then it is probably the responsibility of the creating app (or the user) to purge older snapshots. (At least, that’s what I would expect.)
1 Like

yes, you are right. thank you for your reply.

BTW you can delete all of the local snapshots at once like this:

#!/bin/zsh -f

tmutil listlocalsnapshots / \
| fgrep -i 'com.apple.TimeMachine' \
| sed 's#.*\.##g' \
| sort -u \
| while read line
do

	tmutil deletelocalsnapshots "$line"

done

exit 0
# EOF

I thought there was a way to use something like tmutil deletelocalsnapshots 99999999 but now I can't seem to find any reference to it.

1 Like

problem solved ! thank you very much

as per below, time machine snapshots are taking more and more space on my primary SSD.
it's funny that the only way to list the snapshots with their size is to go through the CCC GUI

The ideal macro which I wanted to create
1- list snapshots and their size
2- prompt user input:
escape to cancel macro
return to continue with your deletion script above.
I have all of the above set up except that for some bizarre reason it is not possible to list the size of the snapshots using a terminal command.

So I have to resort to look at CCC, and then decide if I use your script to delete. It's not a problem. Just strange that I can't do it all with scripting.

i found a solution: I will incorporate view CCC primary SSD window into the macro. So all is fine

the script works perfectly . thank you !

I mentioned this to the developer of Carbon Copy Cloner and he indicated that:

a) It's complicated to calculate

b) Deleting one doesn't necessarily mean you'll get that much space back

c) He might add the ability to see sizes in the command-line version of CCC in the future.

1 Like

thank you very much for contacting him and the info !

my experience is similar to what I read in forums: yes you do regain the space but for some bizarre reason after rebooting your computer and waiting some kind (in the next few days)
I was excited to test your script which in my case creating 15 to 20 GB free space, and stupidly forgot to check disk free space before and after !