ZFS Finding Datasets with the most snapshots

Following up on Removing old ZFS snapshots, I had way too many snapshots... These commands will find all snapshots within the pool tank and count them by dataset.

Following up on Removing old ZFS snapshots, I had way too many snapshots and while I knew some datasets that had too many, I wasn't clear how to find datasets with way to many snapshots that I had to delete.

Simply running zfs list  -t snapshot -o name -s name -r nas resulted in tens of thousands of results I couldn't scroll through. And I wanted to be cautious where I ran zfs destroy on snapshots!

So I decided to create a series of commands that let me count the number of snapshots each dataset has and sort them!

These commands will find all snapshots within the pool tank and count them by dataset

zfs list  -t snapshot -o name -s name -r tank | sed 's/@.*//' | sort | uniq -c | sort

While those are sorted in ascending order, that works nice for me in the terminal. If you prefer just the top datasets in descending order just reverse the final sort and pipe to head!

zfs list  -t snapshot -o name -s name -r tank | sed 's/@.*//' | sort | uniq -c | sort -r | head

 23161 tank/dataset1
 11353 tank/dataset2 
 1126 tank/backup/ubuntu_g1f5ds/var/lib/230948234aef887o90897f987e987e
 1073 tank/dataset4
 953 tank/dataset5
zfs list -t snapshot -o name -s name -r tank | sed 's/@.*//' | sort | uniq -c | sort -r | head 

Warning, this should not be piped to zfs destroy, xargs, or anything like that. Here sed removes the snapshot name leaving just the dataset name.