2

Is there a recommended and deterministic way to delete resources created by a subscription level deployment ?

So far I see a straightforward way only when the subscription level deployment contains a single resource group. In that case deleting the RG will correctly delete all the resources.

However, if there are several RG-s created by the subscription level deployment, the order of deleting them should be driven by dependency order, and discovering that is not easy. Further on, the subscription level deployment may create other resources as well, listed here.

I could not find any good method for that so far.

VenkateshDodda
  • 4,723
  • 1
  • 3
  • 12
tzp
  • 544
  • 7
  • 10

2 Answers2

1

Azure don't have any way to delete the resources that were deployed at subscription level deployment directly.

Alternative way that you can look for

  1. Deployments-Get API will give you the resource ids that were deployed as part of deployment.

GET https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Resources/deployments/{deploymentName}?api-version=2021-04-01

  1. collect all the resource ids from (1)

  2. Use 'Resources - Delete By Id' API to delete resource ids (from 2)

DELETE https://management.azure.com/{resourceId}?api-version=2021-04-01

You can use above approach/logic to build a script or a piece of code to automate whole process.

VenkateshDodda
  • 4,723
  • 1
  • 3
  • 12
  • 1
    But I must delete the deployment's resources in proper order, otherwise the deletion may fail due to dependency problems, right ? My question was mainly aimed at how to find out the deletion order, unless there is some easier way. – tzp Jun 28 '21 at 10:08
0

The resources are generally stored in the order they were deployed. For example, the array of resources in the deployment object would be in the order the resources were deployed. If you delete the resources in the reverse order, This could prevent the deletion from failing.

As stated in the other answer, you can get the deployment, get the resources, reverse the order of the resources, and then delete them by their ID. This is not a foolproof method but it would work in many cases.

For example, here is a bash script that will delete all resources deployed in a subscription level deployment that has name 'deploymentName'. The array of resources is reversed, thus the resources are deleted in the reverse order that they were created (i.e. the last resource created is the first one deleted):
az deployment sub list --query "[? name=='deploymentName'].properties.outputResources[::-1].id" --output tsv | tr $'\t' $'\n' | xargs -d $'\n' -otl az resource delete --ids

john fotouhi
  • 371
  • 3
  • 5