Smart Bash script that will calculate a number of made snapshots per datapool/dataset, based on a number of snapshots, it will decide if snapshots need to be removed or preserved. It will always keep at least two newest created snapshots from each datapool/dataset.

#!/bin/bash
FILESYSTEMS=$(zfs list -t filesystem -o name -H)
for SYSTEM in $FILESYSTEMS; do
SNAPCOUNT=$(zfs list -t snapshot -o name -H | grep "^$SYSTEM@" | wc -l)
TOTAIL=$(($SNAPCOUNT-2))
if [[ $SNAPCOUNT -le 2 ]]; then
continue
else
SNAPSHOTS=$(zfs list -t snapshot -o name -H | grep "^$SYSTEM@" | tac | tail -n $TOTAIL | sort -n)
for SNAPSHOT in $SNAPSHOTS; do
zfs destroy $SNAPSHOT
done
fi
done