Clean up ZFS snapshots/clones after GC

I noticed that I have many ZFS snapshots of my root pool. I suspect that they are created automatically by nixos-rebuild switch. Further, there appear to be clones of those snapshots. How do I clean up old snapshots+clones after nix-collect-garbage without destroying any currently active GC roots?

1 Like

Hum,

I noticed that I have many ZFS snapshots of my root pool. I suspect
that they are created automatically by nixos-rebuild switch. Further,
there appear to be clones of those snapshots. How do I clean up old
snapshots+clones after nix-collect-garbage without destroying any
currently active GC roots?

I stop using zfs on root&c since 17.03 (now I’m happy with nilfs2) and
at this time nothing create automatically nor snapshots nor clones.
However few scripts (outside NixOS) are around since years to mimic
OpenSolaris/IllumOS BEAdm and auto-snapshot service, so perhaps some
of them end up included in NixOS…

Generally speaking you can safely delete any snapshot (zfs destroy -r
or a loop that run zfs destroy on zfs list -t snapshot … output), they
can’t be “active” in any way since they are readonly and their mount
capabilities is limited. On clone I think it’s easy to determinate your
current root volume with a simple zfs mount and deleting the rest should
not brake anything seriously, at maximum the “dummy beadm” script may
complaint that clones it know of are disappeared, nothing that can stop
you booting or using the system though.

That’s to reclaim quickly disk space, after well… Auto-snapshot is
normally a services/systemd timer so you can easy found looking for
services/timers with “snapshot” in it’s name. For the beadm clone I
do not really know if it’s implemented as a kind of nixos-rebuild
hook or else so I’ll have to pass…

Hope that’s help

– Ingmar

nixos-rebuild does not create ZFS snapshots. Those are the result of something else.

You probably have services.zfs.autoSnapshot.enable = true; enabled as mentioned in the wiki. You can change the amount of snapshots created for a given period and they will be automatically removed when the designated systemd timer runs again. You can also include snapshots per dataset, let’s you have your /home in a different dataset you can make configure autosnapshot to only take snapshots of the home dataset while the system dataset that includes the nix store.

Thanks to xte’s hints, I was able to clean things up. (I still don’t know how I got over 600 snapshots with long hash-like names.)

What I did:

  1. zfs send | zfs recv # to another machine to make sure I had a backup
  2. zfs mount # to note the things that are mounted
  3. zfs list -t snapshot
  4. zfs destroy -R
  5. repeat 3-5 until satisfied

The trick was using -R to remove both snapshots and dependent clones. Hope this helps others.