How can I delete tagged resources in a resource group while handling dependencies?

August 5, 2026 3 answers
Asked by MellowPine47
I'm writing a script to find resources in an Azure resource group, check their tag values, and delete only the resources matching a particular tag. The problem is that deletion order matters. For example, Azure won't allow a subnet to be deleted until all resources using it have been removed, so the script fails with a dependency error. The resources were originally deployed with Bicep, but I'm performing the cleanup through a custom script. How should I handle dependent resources when I can't rely on the loop order?

Answers

SilverKite_31
Another option is to group deletion logic by resource type. Delete resources that consume the network resources first, then delete network interfaces, virtual machines, and other attached objects, and handle subnets near the end. Keep the resource type-specific functions explicit rather than relying on the order returned by the resource-group query.

BrightCedar6: Be careful with the tag filter, though: if a dependent resource is excluded because it lacks the tag, the parent resource may still be impossible to delete. The script should detect and report those dependencies instead of repeatedly retrying them.

Reply to this answer
CrispHarbor8
If youโ€™re deleting resources with a custom script, youโ€™ll need to account for the dependency order yourself. First collect the candidate resources and show them for confirmation, then delete them in a known sequenceโ€”for example, workloads and network interfaces before subnets. Afterward, query the resource group again to confirm that everything expected was removed. Bicep created the dependency relationships, but it doesnโ€™t automatically make an arbitrary deletion script dependency-aware.

MellowPine47: That makes sense. Everything was deployed with Bicep, but Iโ€™m still using a script for the cleanup, so Iโ€™ll need to reproduce the relevant order there.

Reply to this answer
QuietMaple22
A retry approach can work if all dependent resources have the tag youโ€™re targeting. Treat dependency failures as non-fatal, add those resources to a retry list, and make another pass after the first batch finishes. You can repeat this a limited number of timesโ€”say 5 to 10 passesโ€”to handle chained dependencies without allowing the script to run forever. Anything still failing after that should be reported for manual review.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions