Snapshots, if left undetected will grow and consume space as well as slow your VMs down if you have enough of them. This is an endless battle for the system administrator as (in my experience) developers will want to keep snapshots forever. This often leads to the same “snapshots are not backups” conversation.
In this post I will detail how to detect and deal with VMWare snapshots using powershell.
How many snapshots do I have?
To get a quick view of snapshots in you environment just issue this command.
get-vm | get-snapshot
You will get a very helpful output of the name of the snapshot and ts description (usually empty)
Not much use. to see what attributes the snapshot object has we just pump that in to a get-member cmdlet (or GM)
get-vm | get-snapshot | gm
This will give us the attributes of the snapshot object so we can hopfuly get something more helpful.
mmmm vm, SizeGB and created look useful so we can re-write the first command and select those attributes.
get-vm | get-snapshot | select VM, name, Created, SizeGB
Much more helpful. We have the VM name, creation date and the size.
We can make it more useful by sorting by size or created date.
get-vm | get-snapshot | select VM, name, Created, SizeGB | sort sizeGB -Descending
get-vm | get-snapshot | select VM, name, Created, SizeGB | sort created
What Snapshots can I delete?
Deciding what snapshots to delete is up to you. but as a guide here I will assume any snapshot called temp can go, and anything created more then half a day ago.
In a production environment probably anything older then 120 days could go. but for this example anything older then half a day is going to be deleted.
So this time instead of using a select cmdlet we use the where-object so our list of snapshots is only what we want to delete.
get-vm | get-snapshot | where{$_.name -eq "temp"}
or lets get old snapshots
get-vm | get-snapshot | where{$_.created -lt (get-date).adddays(-0.5)}
The above two bits of code will give me.
Oh course we can pipe that in to the same select statement we used earlier to get the VM name, size and creation date.
Deleting snapshots.
Now we have identified the snapshots we want to delete we just simply pipe the above command in to the remove-snapshot cmdlet.
The following switches are useful when using remove-snapshot
- -WhatIf will pretend to delete snapshots but not really delete anything, handy for testing.
- -confirm:$true This is the default, Powershell will prompt for every snapshot deletion.
- -confirm:$false When you are happy you are deleting the correct snapshots and you dont want to confirm 100 deletions, use this.
- -RunAsync don’t wait for the snapshot to be deleted. Not recommended if you are deleting lots of snapshots.
The cautious would use this to remove all snapshots older then 120 days and be prompted for each snapshot.
get-vm | get-snapshot | where{$_.created -lt (get-date).adddays(-120)} | Remove-Snapshot -confirm:$true
While the more gun-ho administrators would run the following to remove all snapshots called “temp” with no prompting.
get-vm | get-snapshot | where{$_.name -eq "temp"} | Remove-Snapshot -confirm:$false
Well, that’s all there is to detecting and removing snapshots. I hope the above helps someone out. And remember “Snapshots are not backups!”