VMware

New Scheduled Task to Delete Snapshots in vSphere 8.0 Update 3

Learn about the new scheduled task feature in vSphere 8.0 Update 3 that allows you to delete snapshots on virtual machines in vSphere Client and PowerCLI

In case you have not seen as of yet, one of the new features that is a welcomed addition to vSphere 8.0 Update 3 is a new built-in scheduled task feature that gets rid of snapshots on virtual machines. This is a great new feature that is built-in without having to jump through hoops with other tools or scripts. However, as we will see, the new task can also be extremely handy with PowerCLI and other tools.

Scheduled task to delete snapshots

New with vSphere 8.0 Update 3, you can delete snapshots for virtual machines using a new Snapshot Deletion scheduled task. A new scheduled task can be scheduled to delete snapshots on a virtual machine that are older than a certain number of days.

You will see this if you look at the Snapshots tab for a virtual machine. You will see Snapshot Deletion where you can jump right into the schedule a task link.

Snapshot deletion scheduled task in vsphere 8.0 update 3
Snapshot deletion scheduled task in vsphere 8.0 update 3

After you click the schedule a task you will be taken to the following Scheduled Tasks screen. Click the New Scheduled Task drop down and you will see the option to Delete snapshots.

Selecting the delete snapshots in scheduled tasks
Selecting the delete snapshots in scheduled tasks

The Schedule New Tasks (Delete Snapshots) wizard will launch. Here you can configure the following:

Schedule new tasks delete snapshots wizard
Schedule new tasks delete snapshots wizard

In case you are wondering on the run frequency what the options are, these include the following:

  • Once
  • After vCenter startup
  • Hourly
  • Daily
  • Weekly
  • Monthly
Configuring the frequency of the scheduled task
Configuring the frequency of the scheduled task

Also, after you select on of the options, the configuration for the specific interval will change based on what you select. For example, below is the configuration for the monthly run:

Configuring the schedule for a monthly run
Configuring the schedule for a monthly run

Using PowerCLI

We can also use PowerCLI to invoke the API to do the same. Note the following:

To use PowerCLI to interact with the RemoveAllSnapshots_Task() method and specify the retentionDays property in vSphere 8.0 Update 3, we can use the following:

  1. Connect to the vCenter Server: Connect to your vCenter Server using PowerCLI. Connect-VIServer -Server <vCenter_Server> -User <Username> -Password <Password>
  2. Retrieve the Virtual Machine: Get the virtual machine object you want to interact with $vm = Get-VM -Name "<VM_Name>"
  3. Set Up the Snapshot Selection Specification: Use SnapshotSelectionSpec and set the retentionDays property $vmView = $vm.ExtensionData # Create the SnapshotSelectionSpec object $snapshotSelectionSpec = New-Object VMware.Vim.SnapshotSelectionSpec $snapshotSelectionSpec.RetentionDays = 30 # Set retention days to 30
  4. Use the RemoveAllSnapshots_Task Method: Use the RemoveAllSnapshots_Task() method with the SnapshotSelectionSpec object $task = $vmView.RemoveAllSnapshots_Task($snapshotSelectionSpec)
  5. Task monitoring: You can use the task object to monitor the progress of removing the snapshot operation $taskView = Get-View -Id $task while ($taskView.Info.State -eq "running" -or $taskView.Info.State -eq "queued") { Start-Sleep -Seconds 5 $taskView.UpdateViewData() } # Check the task result if ($taskView.Info.State -eq "success") { Write-Output "Snapshots older than $($snapshotSelectionSpec.RetentionDays) days removed successfully." } else { Write-Output "Failed to remove snapshots: $($taskView.Info.Error.LocalizedMessage)" }

Here is the complete script. Replace the RetentionDays with the number of days you want to use.

# Connect to the vCenter Server
Connect-VIServer -Server <vCenter_Server> -User <Username> -Password <Password>

# Retrieve the Virtual Machine
$vm = Get-VM -Name "<VM_Name>"

# Access the Underlying vSphere API Object
$vmView = $vm.ExtensionData

# Create the SnapshotSelectionSpec object and set the retentionDays property
$snapshotSelectionSpec = New-Object VMware.Vim.SnapshotSelectionSpec
$snapshotSelectionSpec.RetentionDays = 30  # Set retention days to 30

# Invoke the RemoveAllSnapshots_Task Method
$task = $vmView.RemoveAllSnapshots_Task($snapshotSelectionSpec)

# Monitor the Task
$taskView = Get-View -Id $task
while ($taskView.Info.State -eq "running" -or $taskView.Info.State -eq "queued") {
    Start-Sleep -Seconds 5
    $taskView.UpdateViewData()
}

# Check the Task Result
if ($taskView.Info.State -eq "success") {
    Write-Output "Snapshots older than $($snapshotSelectionSpec.RetentionDays) days removed successfully."
} else {
    Write-Output "Failed to remove snapshots: $($taskView.Info.Error.LocalizedMessage)"
}

Troubleshooting

You don’t have the new functionality available? Make sure you have updated your environment to VMware vSphere 8.0 Update 3 and can verify you see the new screens in the vSphere Client.

Make sure you have the proper permissions available to interact with VMware vSphere virtual machines and snapshots.

Wrapping up

The new built-in scheduled task to remove snapshots in vSphere 8.0 Update 3 is a great new scheduled task feature that is built in. Right from the vSphere Client, you can schedule tasks to delete snapshots on specific VMs. As we have seen as well, you can use PowerCLI to invoke the API and use the new functionality in a script or with automation tasks already configured.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.