How to delete Azure Backup Vault with Azure SQL Long Term retention databases

I’ve came across this problem where I had a backup vault I want to delete, but it contains Azure SQL long term backup databases. And you cannot remove them using the portal. Azure Backup is pretty counter-intuitive so I’ve decided to convert that knowledge into a post.

Obviously you will lose all your backups if you do this. But since I was just tinkering around I needed to do that.

What you have to do is remove the backup items and delete the vault after.

# We need to set recovery services context to work with recovery vault
$vault = Get-AzureRmRecoveryServicesVault -Name '{VaultName}'
Set-AzureRmRecoveryServicesVaultContext -Vault $vault
# Get protection containers to work with those
$containers = Get-AzureRmRecoveryServicesBackupContainer -ContainerType AzureSQL -Status Registered
$item = Get-AzureRmRecoveryServicesBackupItem -Container $containers -WorkloadType AzureSQLDatabase
$availableBackups = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $item
$availableBackups # check existing items
foreach ($container in $containers) {
    $items = Get-AzureRmRecoveryServicesBackupItem -container $container -WorkloadType AzureSQLDatabase
    ForEach ($item in $items) {
        Disable-AzureRmRecoveryServicesBackupProtection -item $item -RemoveRecoveryPoints -ea SilentlyContinue
    Unregister-AzureRmRecoveryServicesBackupContainer -Container $container
Written on August 3, 2017