Deleting an Azure Recovery Services Vault with all Backup Items

When I am prototyping Azure solutions with Azure Resource Manager templates and scripts, I often find myself allocating resources and when I am done, I will delete the entire resource group to clean up. If you have ever created some backups in Azure Recovery Services Vault you will know that you cannot simply remove the Azure Recovery Services vault without removing any backup items that you have in there. The details on how to do that can be found here. It is however a bit painful to click your way through deleting a lot of backup items, so I use a PowerShell script. It looks something like this:

First make sure you login:

[ps]
Login-AzureRmAccount
[/ps]

or

[ps]
Login-AzureRmAccount -Environment AzureUSGovernment
[/ps]

if you are using Azure Government

Then create a script, e.g. DeleteRecoveryServicesVault.ps1:

[ps]
param
(
[Parameter(Mandatory=$true,Position=1)]
[String]$ResourceGroupName,

[Parameter(Mandatory=$true,Position=2)]
[String]$VaultName
)

$rv = Get-AzureRmRecoveryServicesVault -Name $VaultName -ResourceGroupName $ResourceGroupName
Set-AzureRmRecoveryServicesVaultContext -Vault $rv
$rcs = Get-AzureRmRecoveryServicesBackupContainer -ContainerType AzureVM

foreach ($c in $rcs) {
$bi = Get-AzureRmRecoveryServicesBackupItem -Container $c -WorkloadType AzureVM
Disable-AzureRmRecoveryServicesBackupProtection -Item $bi -RemoveRecoveryPoints -Force
}

Remove-AzureRmRecoveryServicesVault -Vault $rv
[/ps]

And you can remove the vault with:

[ps]
.\DeleteRecoveryServicesVault.ps1 -ResourceGroupName <RESOURCE GROUP> -VaultName <VAULT NAME>
[/ps]

Of course, you may need to make adjustments depending on what types of workloads you have in the vault. Keep the script handy for when you need to do that again.

Be careful. This script will wipe out your recovery services vault. Make sure that is what you want to do.