How to remove all deployed resources based on deployment name in Azure How to remove all deployed resources based on deployment name in Azure azure azure

How to remove all deployed resources based on deployment name in Azure


The general guidance from Microsoft is that a Resource Group contains zero or more resources that share a common lifecycle. Hence, they would probably tell you to separate different deployments into different Resource Groups.

I have actually tried the same thing you have before, but deleting a deployment only deletes the deployment metadata, not the actual resources that were provisioned by the deployment. It would be a great feature request to be able to "slice and dice" resources, based on the most recent deployment that they were a member of.

Here is the supporting documentation:

All of the resources in your group should share the same lifecycle. You will deploy, update and delete them together. If one resource, such as a database server, needs to exist on a different deployment cycle it should be in another resource group.

https://azure.microsoft.com/en-us/documentation/articles/resource-group-overview/#resource-groups

enter image description here


You can do this if you want to roll up your sleeves and write a bit more code... Though Trevor Sullivan has the best suggestion for overall management of resources.

Take a look at this cmdlet:

(Get-AzureRmResourceGroupDeploymentOperation -DeploymentName $DeploymentName -ResourceGroupName $RGName).Properties.ProvisioningOperation(Get-AzureRmResourceGroupDeploymentOperation -DeploymentName $DeploymentName -ResourceGroupName $RGName).Properties.TargetResource.id

The first will tell you if the operation was a create operation on the resource, the second will give you the resourceId which you can then use to delete with:

Remove-AzureRMResource

But if you organize your resource groups by life cycle then removing the entire group is easier.

The other thing to watch out for here is resources that have dependencies on one another. I'm not sure what will happen in those cases (fail to delete, etc). I can't think of a specific problem to watch out for, just that I haven't spent much time looking at "clean up" this way...


To remove all the deployed resources under a specific resource group,

you should use the Azure PowerShell command:

Remove-AzureRmResourceGroup [-Name] <ResourceGroupName> [-Force <SwitchParameter>]

The Remove-AzureRmResourceGroupDeployment only removed the specific deployment by name and resource group name but not the resources.

Hope this helps!