Unable to delete Azure Subnet due to Resources

PR Code - Christopher Pateman
2 min readApr 14, 2021

--

With Azure Subnets there is an order to deleting the resources. You must disconnect the Virtual Network Subnets before deleting the resources, or you can’t delete the Subnet. This caused myself some issue, while using Terraform as I kicked a Destroy command and did it in the wrong order. However, I found a method on how to correct it.

If you have this issue then you will be presented with an error like this:

Failed to delete subnet
Failed to delete subnet ‘app_subnet’. Error: Subnet app_subnet is in use by cp-rg/providers/Microsoft.Network/virtualNetworks/cp-vnet/subnets/app_subnet/serviceAssociationLinks/AppServiceLink’>cp-vnet/app_subnet/AppServiceLink and cannot be deleted. In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.

The standard method to correct this is to recreate the resource. If you can recreate the resource that was in the Subnet before, then you can reattach it to the subnet. This then brings the resources back to the state they were before. From here you can then Disconnect the Resource and then delete the Subnet.

However, in my case I did not remember what the original resources name was, which means I cannot create it like for like to fix the issue. You can still find the name or names out by using the AZ CLI, which you might also be able to use the API as well.

$rgname = "cp-rg" 
$subnet = "app_subnet" $vnet = "cp-vnet" az network vnet subnet show --resource-group $rgname --name $subnet --vnet $vnet

This then returns all the details of the subnet, including the ‘serviceAssociationLinks’ like below inlcuding the names of the services.

{ "serviceAssociationLinks": [ { "allowDelete": false, "etag": "W/\"00000000-0000-0000-0000-000000000000\"", "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cp-rg/providers/Microsoft.Network/virtualNetworks/cp-vnet/subnets/app_subnet/serviceAssociationLinks/AppServiceLink", "link": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cp-rg/providers/Microsoft.Web/serverfarms/cp-asp-666", "linkedResourceType": "Microsoft.Web/serverfarms", "locations": [], "name": "AppServiceLink", "provisioningState": "Succeeded", "resourceGroup": "cp-rg", "type": "Microsoft.Network/virtualNetworks/subnets/serviceAssociationLinks" } ] }

You can now recreate the resource and delete the subnet.

Originally published at http://prcode.co.uk on April 14, 2021.

--

--

PR Code - Christopher Pateman
PR Code - Christopher Pateman

Written by PR Code - Christopher Pateman

I’m a Azure DevOps Engineer with a wide knowledge of the digital landscape. I enjoy sharing hard to find fixes and solutions for the wider community to use.

No responses yet