Removing old ZFS snapshots

I back up multiple computers to my Truenas instance hourly, but these snapshots hang around without an easy way to prune them (computers I back up include Ubuntu and Proxmox).

But over a couple years one of my datasets seemed to grow snapshots faster than bunny rabbits reproduce to over 400k snapshots!

zfs list -t snapshot -o name -s name -r tank/UBUNTU_DATASET_NAME | wc -l

Not only that, but the above command took several minutes!!! (Taking minutes to list the snapshots was actually how I knew something was wrong.)

Sanoid and syncoid are great, but I was sending too many snapshots over without cleaning them up!

How to clean up snapshots

If you do this be VERY careful that you know what you're deleting. Step 2 is included to make sure we actually look at what we're deleting - don't skip it!

I am not responsible for any commands you run. These commands are not suggestions, just a list of what I ran.

For the below, I'm assuming my pool is named tank and the dataset I want to clean up is called UBUNTU_DATASET_NAME.

  1. I needed to run these as root so I switched to root

sudo su

2. Find the snapshots I need to delete

zfs list -t snapshot -o name -s name -r tank/UBUNTU_DATASET_NAME | grep '@'  | grep _hourly

  • zfs list -t snapshot -o name -s name -r tank/UBUNTU_DATASET_NAME recursively lists the snapshots
  • grep '@' is my safety check to make sure I'm listing snapshots
  • grep _hourly makes sure I'm only deleting the hourly snapshots

3. Pipe those to xargs -n1 zfs destroy

zfs list -t snapshot -o name -s name -r tank/UBUNTU_DATASET_NAME | grep '@'  | xargs -n1 zfs destroy