Last active
January 24, 2024 04:30
-
-
Save SkyWriter/58e36bfaa9eea1d36460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
Hi
The reason this script (and surprisingly, all other scripts which fulfil the same task) has to iterate over the snapshots in reverse order and recheck the size is, that you are using the "used" property instead of the "written" property. The latter is exactly then zero when the snapshot is empty. And in case you wonder, it's from 2011: https://www.illumos.org/issues/1645