Delete all snapshots over 30 days old

8,646

Solution 1

You can do this easily with powercli, as there is a 'remove-shapshot' cmdlet:

$oneMonthAgo = (Get-Date).AddDays(-30)
Get-VM | Foreach-Object {
Get-Snapshot -VM $_ | Foreach-Object {
if($_.Created -lt $oneMonthAgo) {
Remove-Snapshot $_ -Confirm -WhatIf
}}}

I put the -Confirm and -WhatIf in there because Remove-Snapshot could potentially do a lot of damage -- you want to make sure it's targeting the right snapshots before taking those parameters out.

Solution 2

$oneMonthAgo = (Get-Date).AddDays(-30)
Get-VM | Foreach-Object {
Get-Snapshot -VM $_ | Foreach-Object {
if($_.Created -lt $oneMonthAgo) {
Remove-Snapshot $_ -Confirm:$false
}}}

I guess the above script will do and yes add it to task scheduler which will still ease of the work. Recommend to delete the snapshots that are 3 days old.

Share:
8,646

Related videos on Youtube

jacob d davis
Author by

jacob d davis

Interests include: -Java -Python -Web Design

Updated on September 18, 2022

Comments

  • jacob d davis
    jacob d davis over 1 year

    I'm currently using VMware 5.1 and looking for a method to keep snapshots that users create from becoming too old. Is there any tool inside of VMware that allows you to manage snapshots (or possibly a method to script this)?

    Ideally I'd like to delete any snapshots that become over a month old automatically.

  • jacob d davis
    jacob d davis over 10 years
    Shouldn't instead of -lt it be greater than in your code snippet? Aside from that however this is exactly what I'm looking for!
  • 1.618
    1.618 over 10 years
    Logically you would think that, but it compares the dates numerically, so earlier = lower value.