Dmitry Andrievsky
Dmitry Andrievsky

Reputation: 1873

AZURE Resource Manager: Clean up resource group

TL;DR:

Is there a way to delete all resources in resource group except for Complete-mode deploy with empty template and manual step-by-step deletion from web interface?

Delete resource group is not an option (access rights: Contributor, not owner)

Details:

I am trying to completely clean up resource group in AZURE.

Delete resource group is not an option (access rights: Contributor, not owner)

Most elegant and obvious way to do this (also described in some articles) is to perform Complete deployment with "empty" deployment template:

New-AzureRmResourceGroupDeployment -Mode Complete -ResourceGroupName RG_NAME -TemplateFile .\empty.json

with the following empty.json:

{ "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "2018-02-15", "parameters": {}, "variables": {}, "resources": [], "outputs": {} }

But... This works sometimes. Even not most of the time.

Once it got an infinite loop trying to get SQL Server deleted, failed and trying to delete again. After 20 minutes of waiting I went to events and found about 100 events like

"Delete SQL Server Started" ... "Accepted" ... "Failed" ... "Started" ... "Accepted" ... "Failed" ... ...

You got the idea.

Other times it works fine.

So, the question is: is there a way to delete all resources in resource group except for Complete-mode deploy with empty template and manual step-by-step deletion from web interface?

Example that seems to be repeatable

  1. Try to deploy SQL server with the following template (it will fail with password complexity validation)

    { "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "2018-02-19", "parameters": { "location": { "type": "string", "defaultValue": "West Europe" }, "dbAdministratorLogin": { "type": "string", "defaultValue": "sysadm" }, "dbAdministratorPassword": { "type": "string" , "defaultValue": "123123" }, }, "variables": { "dbServerName": "dka-tst-sto-sql-srv", "databaseName": "dka-tst-sto-sql" }, "resources": [ {"comments": "server and database. Database - child resource of a server", "type": "Microsoft.Sql/servers", "name": "[variables('dbServerName')]", "apiVersion": "2015-05-01-preview", "location": "[parameters('location')]", "properties": { "administratorLogin": "[parameters('dbAdministratorLogin')]", "administratorLoginPassword": "[parameters('dbAdministratorPassword')]", "version": "12.0" }, "resources": [{ "type": "databases", "name": "[variables('databaseName')]", "apiVersion": "2014-04-01-preview", "location": "[parameters('location')]", "properties": { "collation": "SQL_Latin1_General_CP1_CI_AS", "edition": "Standard", "maxSizeBytes": "268435456000", "requestedServiceObjectiveId": "f1173c43-91bd-4aaa-973c-54e79e15235b", "sampleName": "", "zoneRedundant": false }, "dependsOn": [ "[concat('Microsoft.Sql/servers/', variables('dbServerName'))]" ] }, { "type": "firewallrules", "name": "AllowAllWindowsAzureIps", "apiVersion": "2014-04-01-preview", "location": "[parameters('location')]", "properties": { "endIpAddress": "0.0.0.0", "startIpAddress": "0.0.0.0" }, "dependsOn": [ "[concat('Microsoft.Sql/servers/', variables('dbServerName'))]" ] } ] } ], "outputs": { } }

Then try to delete with "empty" template.

You'll get infinite loop of DB Deletion "Started .. Accepted .. Failed .. Started .. Accepted .. Failed .. Started ..."

Upvotes: 0

Views: 1694

Answers (1)

4c74356b41
4c74356b41

Reputation: 72171

First and most obvious way of doing this: delete the resource group. recreating it takes 5 seconds, an empty resource group isnt worth much, you can script tags\permissions to reapply them.

Another way could be this:

Get-AzureRmResource | ? ResourceGroupName -eq your_resource_group_name | Remove-AzureRmResource -WhatIf

You can remove -WhatIf when you are confident this only targets the resources you want. You can also use -AsJob to speed deletion up

Upvotes: 2

Related Questions