0

I have some code that uses the Python Azure SDK to deploy a virtual machine within a resource group. I manually provision each resource in order (a vnet and subnet if necessary, a public IP address, a NIC, and finally the VM itself).

Now, when I want to delete the VM, I can query the list of resources within the resource group and filter that list in my code to match only those resources which have a tag with the matching value.

The problem is that you can't just arbitrarily delete resources that have dependencies. For example, I cannot delete the NIC because it is in use by the virtual machine; I can't delete the OS disk because it's also in use by the VM; I can't delete the public IP address because it's assigned to the NIC; etc.

In the Azure portal you can check off a list of resources and ask the portal to delete all of them, and it handles any resource inter-dependencies for you, but it looks like this is not possible from the SDK.

Right now my only solution is to be fully aware of the path of resource creation and dependency within my code itself. I have to work backwards - first, search the list for VMs with the right tag, delete them, then search for disks with the tag, delete them, NICs, and so on down the line. But this has a lot of room for error and is not in any way reusable for other types of resources.

The only other alternative I can think of is "try to delete it and handle errors" but there's a lot of ugly edge cases I could see happening here and I'd rather take a less haphazard way of handling this, especially since we're deleting things.

TL;dr: Is there a proper way to take a list of resources and query Azure to determine which other resources depend on them? (This could be done one resource at a time but it would still be best to have it be "generic" - i.e. able to do this for any resource without necessarily knowing that resource's type up front).

The resource group contains other resources as well which are related to the same project (e.g. other VMs, a storage account, etc.) so deleting an entire resource group is NOT an option.

fdmillion
  • 4,823
  • 7
  • 45
  • 82

1 Answers1

-1

One of the workarounds that you can try is using Azure Powershell and tags. Try adding the tags to the resources that you wanted to delete and then use the below command to delete the resources in bulk.

$resources = az resource list --tag Key=Value| ConvertFrom-Json

foreach ($resource in $resources) {
    az resource delete --resource-group $resource.resourceGroup --ids $resource.id --verbose
}

This will delete the resources regardless the location or the resource group where it has been created.

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • I don't think that will work since it's still issuing individual delete commands per resource. So if it happens ti try to delete the NIC first, but the NIC is still attached to the VM, deleting the NIC will fail. I could just run through the loop over and over until there are no resources left, but Azure SDK commands are sloooooow, so that'll just make things take longer. – fdmillion Jan 05 '22 at 09:02