Remove stuck VM in Azure Recovery Services vault

Category

Tips & Tricks

Published

February 24, 2017

I came across an interesting error in a customer environment the other day when configuring a Recovery Vault in Azure. The Azure Backup Recovery vault is the built in backup/restore service that Azure provides for Virtual Machines. Sometimes you delete VM's and also delete the complete Recovery Vault to move it somewhere else.

Problem

This is what I did; I deleted the VM's (since they were temporary test servers) and then removed them from the Recovery Vault. All worked fine. Although, when I wanted to delete the Recovery Vault itself it gave me an error, even if i tried from PowerShell, portal and all kinds of different ways. Somehow one of the VM's "hang around" in the Recovery Vault although I couldn't see it when I listed resources in the Recovery Vault. There was no data in the vault, no servers, no nothing.

The error I got was this (from PowerShell using the AzureRM PowerShell module):


PS C:\> get-azurermrecoveryservicesvault -ResourceGroupName MYRESOURCEGROUP

Name : MYRECOVERYVAULT
ID : /subscriptions/MYSUBSCRIPTIONID/resourceGroups/ MYRESOURCEGROUP/providers/Microsoft.RecoveryServices/vaults/ MYRECOVERYVAULT
Type : Microsoft.RecoveryServices/vaults
Location : northeurope
ResourceGroupName : MYRESOURCEGROUP
SubscriptionId : MYSUBSCRIPTIONID
Properties : Microsoft.Azure.Commands.RecoveryServices.ARSVaultProperties

PS C:\> $vault=(get-azurermrecoveryservicesvault -ResourceGroupName MYRESOURCEGROUP)

PS C:\> Remove-AzureRmRecoveryServicesVault -Vault $vault

Remove-AzureRmRecoveryServicesVault : Operation Failed.
ErrorCode: ServiceResourceNotEmpty
Message: Vault cannot be deleted as there are existing resources within the vault.
Please delete any registered servers, Hyper-V sites (Used for Site Recovery), policy associations for SCVMM clouds (Used for Site Recovery) and then delete the vault.
At line:1 char:1
+ Remove-AzureRmRecoveryServicesVault -Vault $vault
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzureRmRecoveryServicesVault], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.RecoveryServices.RemoveAzureRmRecoveryServicesVault

When using ARMClient.exe I got the same error:


“error”: {
	“code”: “ServiceResourceNotEmpty”,
  ”message”: “Vault cannot be deleted as there are existing resources within the vault.\nPlease delete any registered servers, Hyper-V sites (Used for Site Recovery), policy associations for SCVMM clouds (Used for Site Recovery) and then delete the vault.”,
  ”target”: null,
  ”details”: null,
  ”innerError”: null
}

Solution

After some investigation I managed to solve the problem by doing the following:

  • Get the ARMClient from https://chocolatey.org/packages/ARMClient
  • Login using subscription credential with the command (using cmd prompt): ARMClient.exe login
  • Unregister the hang around VM using following command:
    ARMClient.exe delete /subscriptions/SUBSCRIPTIONNAME/resourceGroups/RESOURCEGROUP/providers/Microsoft.RecoveryServices/vaults/RECOVERYVAULTNAME/registeredIdentities/VMNAMEFQDN?api-version=2016-06-01
  • Delete the Recovery Vault from the portal or PowerShell

Good luck and be careful so you delete the right resources! It took me a while to figure this solution out. :)

Written by

Karl Wallenius