CodeMed
CodeMed

Reputation: 9205

Destroy resources created by ARM template deployment

What specific syntax needs to be used in order to successfully destroy all resources that were created by the devenvironment deployment group in the code below?

The Context:

A collection of resources are created in Azure when the following command is run:

az deployment group create --name devenvironment --resource-group myResourceGroupDev --template-file C:\path\to\my.template.json --parameters C:\path\to\my.parameters.json --verbose  

The Problem:

The problem we are having is that the following command leaves all the created resources intact in Azure:

az deployment group delete --name devenvironment --resource-group myResourceGroupDev --verbose  

All that this az deployment group delete command does is delete the wrapper devenvironment that associates the resources that were created using the az deployment group create command above. This az deployment group delete command fails to have any effect on the actual resources that were created.

Desired Result:

What we want is a command that will result in destroying all of the resources that were created by the az deployment group create command.

We want a command like terraform destroy that destroys all the resources that were created in an ARM deployment, but using the az cli.

What specific syntax will bring about our desired result?

Upvotes: 6

Views: 6730

Answers (2)

dragonlipz
dragonlipz

Reputation: 101

I have a simpler solution to this. But it does require you destroy everything in the resource group. But then that is the only way Bicep can manage "owning" the resources it creates, is by owning the entire resource group.

I'm doing AKS so if I have a bicep file that defines that and I can do a deployment like so.

az deployment group create \
  --resource-group ${RESOURCEGROUP} \
  --name ${DEPLOYMENT} \
  --template-file ./aks.bicep \
  --parameters "./aks.parameters.json" \
  --mode Complete 

Then to destroy it takes 2 steps. One to remove resources using --mode Complete. And one to delete the deployment group name.

# -- apply empty bicep template file to wipe out the resource group contents
az deployment group create \
  --resource-group ${RESOURCEGROUP} \
  --name ${DEPLOYMENT} \
  --template-file ./empty.bicep \
  --mode Complete 

# -- And now we remove the deployment name 
#    (that appears to have no real value or effect anyway)
az deployment group delete \
  --resource-group ${RESOURCEGROUP} \
  --name ${DEPLOYMENT}

It's ugly but I've not found any other useful, foolproof way to fully destroy resources created with bicep.

Upvotes: 0

VenkateshDodda
VenkateshDodda

Reputation: 5546

We don't have any direct cmdlet to delete all the resources that were deployed in a specific deployment using the arm template.

You can use the below Azure CLI Cmdlets or PowerShell cmdlets which will delete the resources that were deployed using the arm template.

To test this in our local environment, we have created a webapp & app service plan using arm template. Using the below cmdlets we are able to delete those resources successfully.

Here are the Azure CLI cmdlets:

resourcelist=$(az deployment group  show --resource-group '<rgName>' --name '<deploymentName>' --query "properties.outputResources[].id" -o tsv)
for resource in $resourcelist; do az resource delete --ids $resource; done

Here is the sample Output for reference :

enter image description here

Here are the PowerShell cmdlets:

$resourcelist=Get-AzResourceGroupDeploymentOperation -ResourceGroupName '<resourceGroupName>' -Name '<deploymentName>'
foreach ($resource in $resourcelist){
    if( $resource.TargetResource -ne $null){
         Remove-AzResource -ResourceId $resource.TargetResource -Force 
         Write-Output "Resource got delete successfully"$resource.TargetResource
  }
}

Here is the sample Output for reference:

enter image description here

Upvotes: 4

Related Questions