DZone
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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Emulating the History Command Within a Bash Script
  • Keep Your Application Secrets Secret
  • Deploy an Application for Azure Container Registry
  • Git Bash (Or Git in Bash)?

Trending

  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • Why Database Migrations Take Months and How to Speed Them Up
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. 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.

By 
Rahul Rai user avatar
Rahul Rai
DZone Core CORE ·
Mar. 17, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
3.5K 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.

Related

  • Emulating the History Command Within a Bash Script
  • Keep Your Application Secrets Secret
  • Deploy an Application for Azure Container Registry
  • Git Bash (Or Git in Bash)?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!