Bulk Deleting ZFS Snapshots

First off, this post will help alleviate some of the guilt I’ve been having for neglecting my blog.  Second, I just wanted to write it down for my reference.  There’s probably a better way to do this but this worked for me.

I use NexentaStor at home for my ZFS/EXSi All-In-One.  I have a few NexentaStor auto-services set to do ZFS snapshots on each of the filesystems.  I take hourly snapshots kept for a day, daily snapshots that are kept for a week, and weekly snapshots that are kept for a month.  I’ve been having a problem with some of the snapshots not getting expired.  Since I’m using the community edition, I can’t really complain.  I should probably file a bug, but I know the team is busy working on v4.0, which will be awesome.  The admin GUI doesn’t have a way that I can see to remove them, so I delved into the command line.

First, you need a list of all the snapshots on the system:

# zfs list -t snapshot
NAME USED AVAIL REFER MOUNTPOINT
syspool/[email protected] 937M - 1.47G
syspool/[email protected] 204M - 1.90G
syspool/[email protected] 183M - 1.98G
syspool/[email protected] 217M - 2.00G
syspool/[email protected] 421M - 2.01G
syspool/[email protected] 218M - 2.02G
syspool/[email protected] 337M - 2.14G
syspool/[email protected] 398M - 2.10G
[email protected] 18K - 33K
[email protected] 0 - 33K
[email protected] 0 - 33K
[email protected] 0 - 33K
[email protected] 0 - 33K
[email protected] 0 - 33K
[email protected] 0 - 33K
[email protected] 0 - 33K
[email protected]

Now copy the names of the snapshots you want to remove & put them in a text file (mine was zfs_cleanup.txt) like this:

[email protected]
[email protected]
[email protected]
[email protected]

Now you can run a short for loop against the file to remove the snapshots:

#!/bin/bash
file="zfs_cleanup.txt"
while IFS= read -r line
do
# display $line or do somthing with $line
echo "Deleting snapshot $line"
zfs destroy $line
done <"$file"

If you just have a few snapshots to remove, that’s kinda overkill, but I had ~50. Saved me a bit of time. :)

Comments