Recursive removal of ZFS snapshots

Recursive removal of ZFS snapshots for given volume:

#!/bin/bash

if [ "$1" == "" ]; then
 echo "You need to specify volume name, all snapshots will be recursively removed"
 exit 1
fi

snapshots=`zfs list -H -o name -t snapshot -r $1`
lines=`echo "$snapshots" | wc -l`

for snapshot in $snapshots; do
 echo $snapshot
done
read -p "Removing $lines snapshots, write yes to continue: "
if [ "$REPLY" != "yes" ]; then
 echo "Cancel snapshot removal."
 exit 1
fi

i=1
for snapshot in $snapshots; do
 echo "$i / $lines: $snapshot"
 zfs destroy "$snapshot"
 i=$((i+1))
done

exit 0

 

One Comment

  1. because they no longer exist on the sending side, you might consider using the snapshots hold feature. Holding a snapshot prevents it from being destroyed. In addition, this feature allows a snapshot with clones to be deleted pending the removal of the last clone by using the

Leave a Reply to Isabella Cancel reply

Your email address will not be published. Required fields are marked *