How to: Quickly move or delete all resources in an Azure Resource Group

So picture the scenario… You’re working in your Microsoft Azure subscription and you have a Resource Group that you want to delete but it still contains a number of resources. You log in to the Azure portal and find that you can only delete the Resource Group once all the resources within that group have been moved or deleted.

azure_deleteResources

When you have a large number of resources, this can be time consuming and monotonous.

There are a couple of easy ways you can speed up this process by using the following tools:

  • Azure PowerShell Module
  • Azure Resource Explorer

This article shows you how to use the above methods to quickly delete your Resource Groups and all associated resources.

The first thing to note is that management of all Resource Groups is performed using Azure Resource Manager and is not supported with the older Azure Service Management mode.

While ASM doesn’t directly support Resource Groups, all resources created in ASM are automatically allocated to Resource Groups in the background. These are named along the lines of “Default-Networking“, “Default-Storage-WestEurope“, etc. or with the same name as the “Cloud Service” you created.

Azure PowerShell Module

The first approach is to use the Azure PowerShell Module.

Once you have logged in to your Azure account (using “add-azureaccount”), ensure you’re connected to the correct subscription if you have multiple subscriptions (use “Get-AzureSubscription” and check that the subscription you wish to use is set to True against the “IsCurrent” field) and then run the following command:

# Define Resource Group
$ResourceGroupId = "/subscriptions/<subscriptionID>/resourceGroups/<sourceResourceGroupName>"

Remove-AzureResource -ResourceId $ResourceGroupId -ApiVersion 2014-04-01 -Force

Replace <subscriptionID> and <ResourceGroupName> with the relevant values for your environment and don’t forget the “-Force” parameter which is the crucial bit when deleting a Resource Group with associated resources.

It’s important to note at this point that EVERYTHING associated with the Resource Group will be deleted. If you have a Storage Account associated with the Resource Group, this will also be deleted along with ALL CONTENT – that includes your blobs, tables, EVERYTHING. You have been warned!!

So what if you want to move one or more resources to another Resource Group first? Well we have that covered too…

azure_movingResources

# Define source Resource Group
$sourceResourceGroupId = "/subscriptions/<subscriptionID>/resourceGroups/<sourceResourceGroupName>"

#Define target Resource Group and resource(s) to move
$ParametersObject = @{
  targetResourceGroup = "/subscriptions/<subscriptionID>/resourceGroups/<targetResourceGroupName>";
  resources = 
    "<resourceID_1>",
    "<resourceID_2>",
    "<resourceID_3>"
}

# Invoke action to move resource to target Resource Group
Invoke-AzureResourceAction -ResourceId $sourceResourceGroupId -Action moveResources -Parameters $ParametersObject -ApiVersion 2014-04-01 -Force

Note the formatting of the array “$ParametersObject” above which uses a semi-colon instead of a comma after the “targetResourceGroup” variable (the Microsoft examples I’ve found to date show a comma which is incorrect), and the inclusion of comma separated values for entering multiple resource IDs when moving multiple resources in a single operation.


It’s worth noting at this point that I’ve been coming across the following error when trying to move a single resource using the above method (it works fine when moving multiple resources):

Invoke-AzureResourceAction : {
    "Error": {
        "Code": "InvalidRequestContent",
        "Message": "The request content was invalid and could not be deserialized: 'Error converting value 
        "/<resourceID>" to   type 'System.String[]'. Path 'resources', line x, position y.'.",
        "Target": null,
        "Details": null
    }
}

I’m currently looking into this with Microsoft and will provide an update on this post as soon as I have an answer. This doesn’t appear to be a problem when moving resources using Azure Resource Explorer.


Azure Resource Explorer

Ok, so what if you prefer to use a GUI and don’t fancy using PowerShell? Well thankfully the powerful Azure Resource Explorer has you covered.

are_deleteResourceGroups

To delete a Resource Group:

  1. Simply browse to the Azure Resource Explorer site: https://resources.azure.com/
  2. Select your tenant from the drop-down at the top of the page (if you have multiple tenancies)
  3. Ensure the Azure Resource Explorer is in “Read/Write” mode
  4. Browse to the Resource Group you want to delete, under:
    subscriptions > subscriptionName > resourceGroups > ResourceGroupName
  5. Click “Delete” to delete the Resource Group and all associated resources
  6. Click “Yes” on the confirmation

To move a resource to another Resource Group:

  1. Simply browse to the Azure Resource Explorer site: https://resources.azure.com/
  2. Select your tenant from the drop-down at the top of the page (if you have multiple tenancies)
  3. Ensure the Azure Resource Explorer is in “Read/Write” mode
  4. Browse to the Resource Group you want to move resource from, under:
    subscriptions > subscriptionName > resourceGroups > ResourceGroupName
  5. Using the code box, enter the details of the “targetResourceGroup” you wish to move the resources to and the “resources” you wish to move
  6. Click “moveResources” to move the resources to the target Resource Group
  7. Click “Yes” on the confirmation

Note that the targetResourceGroup and resources entries must be entered as a fully qualified resource ID that starts with “/subscriptions/<subscriptionId>“. This can be easily retrieved from Azure Resource Explorer by browsing to the resource or Resource Group and viewing the ID field on the “Data (Get, PUT)” tab:

are_DataTab

When moving multiple resources, this can be achieved by creating a comma separated list of IDs like the following code snippet:

{
   "targetResourceGroup": "/subscriptions/<subscriptionID>/resourceGroups/<targetResourceGroupName>",
   "resources": [
     "/subscriptions/<subscriptionID>/resourceGroups/<ResourcePath>/<ResourceName1>",
     "/subscriptions/<subscriptionID>/resourceGroups/<ResourcePath>/<ResourceName2>",
     "/subscriptions/<subscriptionID>/resourceGroups/<ResourcePath>/<ResourceName3>"
   ]
}

I’m sure this can also be done using the Azure Xplat-CLI, so will update this post when I get a chance to test this.

In the meantime, good luck moving your resources between Resource Groups and deleting Resource Groups… but please be careful when deleting Resource Groups containing Storage Accounts

Kevin is a passionate and driven IT consultant with over a decade of experience in designing and implementing data centre infrastructure… he also has a passion for the coast, and loves kitesurfing!

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud