Powercli to list & delete snapshots

We have realized we are having broken NetApp VMs backup. The process left a lot of VM snapshots. To start off let’s find out those snaps with:

get-vm | get-snapshot  | where {$\_.Description -match "SMVI Snapshot generated for backup"} | Format-Table -Property VM,Name,Created,Description, SizeMB

This command prints the flowing table:

VM        Name         Created              Description        SizeMB
--        ----         -------              -----------        ------
vyuka.fs  smvi\_a3d... 23.11.2012 17:56:25  SMVI Snapshot...  6768,13
ojs       smvi\_a3d... 23.11.2012 18:01:25  SMVI Snapshot...   892,63
netapp-oc smvi\_a3d... 23.11.2012 17:56:04  SMVI Snapshot...  7752,65
tinlib    smvi\_a3d... 23.11.2012 17:59:13  SMVI Snapshot...  7968,11
aptdater  smvi\_a3d... 23.11.2012 17:54:16  SMVI Snapshot...   323,02
jabber    smvi\_a3d... 23.11.2012 18:06:42  SMVI Snapshot...  1211,03
ineerweb  smvi\_a3d... 23.11.2012 17:59:44  SMVI Snapshot... 16496,11

Final command is as follows:

get-vm | get-snapshot  | where {$\_.Description -match "SMVI Snapshot generated for backup"}| Remove-Snapshot -Confirm:$false -RunAsync

BTW: I have successfully shut down a mid-sized cluster with this command. Slow storage?!?


Comments:

Dexter -

you can also use name of snapshot:

get-vm | Get-Snapshot | where {$\_.Name -match "name of snapshot"} | Remove-Snapshot -confirm:$false

Jay -

“BTW: I have successfully shut down a mid-sized cluster with this command. Slow storage?!?” By specifying “-RunAsync” you’re allowing vCenter to process the removal of as many snapshots as it can find (up to its internal limit) simultaneously. If you have enough snapshots - especially if they’re large, heavily nested snaps - then that could certainly have a storage performance impact. To resolve this, you could either remove “-RunAsync” and have each removal run consecutively (ugh, maybe not!) or you could throw an extra check in there to make sure that it only processes a certain number of snaps at any one time.

Jermaine -

You could use the following script:

$maxtasks = 5 $snaps = get-vm | get-snapshot -name "smvi\*" $i = 0 while($i -lt $snaps.Count){ Remove-Snapshot -Snapshot $snaps\[$i\] -RunAsync -Confirm:$false $tasks = Get-Task -Status "Running" | where {$\_.Name -eq "RemoveSnapshot\_Task"} while($tasks.Count -gt ($maxtasks-1)) { sleep 30 $tasks = Get-Task -Status "Running" | where {$\_.Name -eq "RemoveSnapshot\_Task"} } $i++ }

PowerCLI: Delete all snapshots by name ← BAFM -

[…] I was looking into a quick way to delete all those 520 snapshots with PowerCLI, and I found something pretty […]

Remya -

How to delete the latest snapshot first ?

Risma -

How to delete the latest snapshot ?