9
votes

I'm working on provisioning new Azure environment using ARM templates. In order to deploy I use the Azure PowerShell New-AzureRmResourceGroupDeployment command, where I specify DeploymentName , ResourceGroupName etc.

However, when I want to remove the deployed resources by running

Remove-AzureRmResourceGroupDeployment -Name DeploymentName -ResourceGroupName RGname -Force 

it does not remove resources. It just deletes a tag in deployment tab in Azure portal. Is there a way to rollback or remove deployment with related resources? I don't want to delete whole Resource group.

3

3 Answers

11
votes

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

2
votes

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...

-2
votes

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!