Skip to content

Instantly share code, notes, and snippets.

@SkyWriter
Last active January 24, 2024 04:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SkyWriter/58e36bfaa9eea1d36460 to your computer and use it in GitHub Desktop.
Save SkyWriter/58e36bfaa9eea1d36460 to your computer and use it in GitHub Desktop.
#!/bin/bash
fsWithSnapshots=$(zfs list -Hr -t snapshot tank/share |grep '@' |cut -d '@' -f 1 |uniq)
for fs in $fsWithSnapshots ; do
# Changed to now sort newest to oldest. This will mean that newer snapshots without deltas will get removed.
emptySnapshot=$(zfs list -Hr -d1 -t snapshot -o name,used -S creation $fs |sed '$d' |awk ' $2 == "0B" { print $1 }' )
for snapshot in $emptySnapshot ; do
# Added safety check. Verify the size of the snapshot prior to destroying it
used=$(zfs list -Hr -d1 -t snapshot -o used $snapshot)
if [[ $used != "0B" ]]; then
continue
fi
echo "Destroying empty snapshot $snapshot"
zfs destroy $snapshot
done
done
@n0099
Copy link

n0099 commented Jan 24, 2024

https://gist.github.com/SkyWriter/58e36bfaa9eea1d36460?permalink_comment_id=3552336#gistcomment-3552336

Here's a revised version -- it should avoid deleting snapshots that actually have data, and should preserve the first snapshot in which the change appeared:

jimsalterjrs/sanoid#616 (comment)
https://www.reddit.com/r/zfs/comments/ja1k7n/sanoid_dont_create_empty_0b_snapshots/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment