Adventures in ZFS: Deletion of a Recursive Snapshot Set

Adventures in ZFS: Deletion of a Recursive Snapshot Set

Quite often I will find it necessary to create a recursive snapshot over a large number of ZFS file systems.  In this case, I find that frequently have little long term need for them, and need a quick and easy way of disposing of the snapshots created without spending large amounts of time manually deleting all of the snapshots.

Creating Some Snapshots

Here we will create some test snapshots.  Make sure you do this on a test system, in a throw away area, this way if something is mistyped it doesn’t cause any problems.

# zfs snapshot -r tank/zones@testsnap

You can of course generate a date-based name for your snapshots too.

# zfs snapshot -r tank/zones@`date +%m%d%Y.%H%M`

Bulk Deleting Snapshots

Now we can create a simple for loop to identify the snapshots and then perform actions against them.

# for snap in `zfs list -H -t snapshot -r tank/zones | grep "@testsnap" | cut -f 1`; do echo -n "Destroying $snap..."; zfs destroy $snap; echo "  Done."; done

In this case our zfs list finds all the snapshots below tank/zones which have a snapshot named testsnap.  Once we identify all of these the for loop takes over and executes a zfs destroy against each snap.