DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > Delete Multiple Resources and Resource Groups in Azure With Tags

Delete Multiple Resources and Resource Groups in Azure With Tags

You might find it challenging to delete multiple resources and resource groups to entirely remove a service from a subscription. Azure resource tags can help solve that.

Rahul Rai user avatar by
Rahul Rai
CORE ·
Mar. 17, 22 · Cloud Zone · Tutorial
Like (4)
Save
Tweet
2.99K Views

Join the DZone community and get the full member experience.

Join For Free

You might have noticed that resources comprising some Azure services such as Azure Kubernetes Service (AKS) span multiple resource groups by default. In some cases, you might intentionally want to segregate resources such as disks and network interfaces from VMs by placing them in different resource groups for better management. A common problem arising from the resource spread is that you might find it challenging to delete multiple resources and resource groups to entirely remove a service from a subscription.

We can solve the problem by using resource tags to associate resources and resource groups to a service. Tags are key-value pairs that can be applied to your Azure resources, resource groups, and subscriptions. Of course, you can use tags for many other purposes apart from resource management. The Azure docs website has a detailed guide on the various resource naming and tagging strategies and patterns.

Tagging Resources

Let's start with the easiest method to create tags: through the UI. The following screenshot from the AKS cluster creation wizard presents the step to define tags for the resources comprising the AKS cluster:

Specifying Tags for Resources During Service Creation
Specifying tags for resources during service creation

Almost every service creation wizard, including resource groups, allows you to set tags for the resources. The tags you define are applied to the resources and resource groups of the service when they are created. The Azure CLI and PowerShell command to create a service also support specifying tags using the --tags or -TagName parameter, respectively.

You can edit the tags of an existing resource by bringing up the tag editor window of the resource as follows:

Editing Tags of A Resource
Editing tags of a resource

Apart from the UI, you can also use the Azure CLI and PowerShell to apply tags to an existing resource.

Deleting Resources Using Tags

We will create a command that deletes the resources and resource groups that have the tag disposable-service set to true for this demo. Remember that the names of the tags are case insensitive, and the tag values are optional.

Let's start with listing the resource groups that match the constraint using the az group list command as follows:

Shell
 
az group list --tag disposable-service=true --query "[].[id]" --output tsv


The complete list of commands in the az group family is available on the Azure CLI docs website.

You can execute the previous command in the dedicated Azure Cloud Shell or the integrated Azure Cloud Shell by clicking on the Azure Shell icon in the Azure Portal toolbar. Here is the output of the command from my console:

List of Resource Groups
List of resource groups

The following command will delete all the resource groups that we received from the previous command. The parameter --yes allows you to skip the prompt to confirm the deletion.

Shell
 
az group list --tag disposable-service=true --query "[].[name]" --output tsv | xargs -l az group delete --yes --name


After removing the resource groups, we will delete individual resources with the disposable-service tag present in shared resource groups. For this, we will use the az resource list command to list the ids of the resources that have the tag disposable-service set to true as follows:

Shell
 
az resource list --tag disposable-service=true --query "[].[id]" --output tsv


Following is the output of the command:

List of Resources
List of resources

Next, we will use the az resource delete command to delete the identified resources. The command allows you to delete multiple resources simultaneously by setting a space-delimited list of resource ids as the --ids parameter value.

To consolidate the list of resource ids, we will pass the list of resource ids to the tr command, which will replace the newline characters with spaces, and then pass the space-delimited list of resource ids to the resource delete command as follows:

Shell
 
az resource list --tag disposable-service=true --query "[].[id]" --output tsv | tr '\n' ' ' | xargs az resource delete --ids


The final command to delete resource groups and resources enhanced with the ability to handle empty results is as follows:

Shell
 
az group list --tag disposable-service=true --query "[].[name]" --output tsv | xargs --no-run-if-empty -l az group delete --yes --name && \
az resource list --tag disposable-service=true --query "[].[id]" --output tsv | tr '\n' ' ' | xargs --no-run-if-empty az resource delete --ids


azure Command (computing) Listing (computer) shell

Published at DZone with permission of Rahul Rai, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • C++ Creator Bjarne Stroustrup Interview
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • What Is High Cardinality?
  • NextJS Vs React: Key Differences, Advantages and Limitations

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo