I have the issue that we use a cloud automation software, which for whatever reason failed to delete the hypervisor snapshots.
Now I was looking into a quick way to delete all those 520 snapshots with PowerCLI, and I found something pretty quick.
Based on that, I came up with my own quick PowerCLI one-liner, that’ll list all VMs and their snapshots:
1 |
Get-VM | Get-Snapshot | Where { $_.Name -like "201502*" } | Format-Table -Property VM, Name, Created, Description, SizeMB -AutoSize |
Now, I could use another one-liner to delete all snapshots, that’ll look like this:
1 |
Get-VM | Get-Snapshot | Where {$_.Name -like "201502*"} | Remove-Snapshot -Confirm:$false -RunAsync |
However, this will either crash an ESXi host (because the amount of the snapshots is too much) or overwealm the storage. So in the end I used a script like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$maxtasks = 5 $snaps = Get-VM | Get-Snapshot | Where { $_.Name -like "201502*" } $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++ } |
This’ll limit the maximum amount of snapshots to delete at times to 5 (or 10 if you change the $maxtasks value).