New Scheduled Task to Delete Snapshots in vSphere 8.0 Update 3
![New scheduled task delete snapshots](https://www.virtualizationhowto.com/wp-content/uploads/2024/07/New-Scheduled-Task-delete-snapshots-780x470.png)
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.
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.
The Schedule New Tasks (Delete Snapshots) wizard will launch. Here you can configure the following:
- Task name
- Description
- Target
- Run frequency
- Email notification upon completion
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
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:
Using PowerCLI
We can also use PowerCLI to invoke the API to do the same. Note the following:
- retentionDays: dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.vm.SnapshotSelectionSpec.html
- RemoveAllSnapshots_Task API method – dp-downloads.broadcom.com/api-content/apis/API_VWSA_001/8.0U3/html/ReferenceGuides/vim.VirtualMachine.html#removeAllSnapshots
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:
- Connect to the vCenter Server: Connect to your vCenter Server using PowerCLI.
Connect-VIServer -Server <vCenter_Server> -User <Username> -Password <Password>
- Retrieve the Virtual Machine: Get the virtual machine object you want to interact with
$vm = Get-VM -Name "<VM_Name>"
- Set Up the Snapshot Selection Specification: Use
SnapshotSelectionSpec
and set theretentionDays
property$vmView = $vm.ExtensionData # Create the SnapshotSelectionSpec object $snapshotSelectionSpec = New-Object VMware.Vim.SnapshotSelectionSpec $snapshotSelectionSpec.RetentionDays = 30 # Set retention days to 30
- Use the RemoveAllSnapshots_Task Method: Use the
RemoveAllSnapshots_Task()
method with theSnapshotSelectionSpec
object$task = $vmView.RemoveAllSnapshots_Task($snapshotSelectionSpec)
- 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.