WP_20151006_16_27_30_Pro [2417348]

Can’t Delete an Azure Resource Group

Recently, I hit a wall. I wasn’t able to delete my Azure Resource Groups and got quite frustrated… I’m writing this post, with hopes that you may save some time, frustration and energy with your ongoing challenge.

Azure is transitioning from its earlier model to Azure Resource Manager. While we’re in the middle of this appreciable transformation, Classic artifacts are mixed with ARM artifacts. So it’s not surprising that it can be challenging to identify and overcome blockers.

Today I was dealing with a few Resource Groups that I was unable to delete. Troubleshooting error messages as they appeared, the first error message told me that I had Resource Locks, that ultimately prevented me from deleting Virtual Machines. I proceeded to delete the Resource Locks. And finally deleted the Virtual Machines. The next error message notified me that my storage accounts were not empty. Using this information, I found the VHDs. They had active leases and were effectively blocking the Remove-AzureResourceGroup command. I removed the Blob leases and deleted the VHDs from the Storage Accounts.

At this point, I thought I covered all my bases. Boy was I wrong… The next error message was a bit harder to understand.

{
    "Error": {
        "Code": 102,
        "Target": null,
        "Message": "Deletion of resource group 'linuxms' failed because some resources could not be deleted. The tracking Id is '5eec71b0-c97f-4dce-aa33'. Please check audit logs for more details.",
        "Details": null
    }
}

I checked audit logs for more details and found the following:

{
    "error": {
        "code": "StorageAccountOperationFailed",
        "message": "Unable to delete storage account 'briseboismsl': 'Storage account briseboismsl has some active image(s) and/or disk(s), e.g. briseboismsl-briseboismsl-os-1436295942285. Ensure these image(s) and/or disk(s) are removed before deleting this storage account.'."
    }
}

Alright, now I had something concrete to work with. Since I had a single Storage Account and was not worried about keeping any Azure Virtual Machine Images, I used the following to delete them from my Azure Subscription.

Switch-AzureMode -Name AzureServiceManagement

#WARNING: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release.

Get-AzureVMImage `
 | Where-Object {$_.Category -eq 'User'} `
 | ForEach-Object { Remove-AzureVMImage -ImageName $_.ImageName -DeleteVHD }

At this point, I was out of ideas. So I opened a support ticket. The support engineer was quite helpful and reminded me that even though I deleted the VHD blobs from storage, I needed to clear out the Disks using the Remove-AzureDisk cmdlet from Azure Service Management.

Switch-AzureMode -Name AzureServiceManagement

#WARNING: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release.

Get-AzureDisk `
 | ForEach-Object { Remove-AzureDisk -DiskName $_.DiskName  -DeleteVHD -ErrorAction Continue }

Remove-AzureStorageAccount -StorageAccountName 'briseboismsl'

Finally, I was able to deleted the Resource Group from my Azure Subscription.

Switch-AzureMode -Name AzureResourceManager

#WARNING: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release.

Remove-AzureResourceGroup -Name 'linuxms'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s