Allen Walker
Allen Walker

Reputation: 73

How to delete azure resource using tag?

Is it possible to delete my azure resources(in different resource groups) using tag? If possible, how to achieve it in powershell?

I have searched but found nothing related. Maybe I have missed something basic?

Any guidance will be appreciated.

Upvotes: 2

Views: 2779

Answers (3)

Raja Shekar
Raja Shekar

Reputation: 23

$rg= “<Resource group name>”
$tagname = “<Tag name Ex: Env>”
$TagValue =”<Tag Value Ex: Dev>”
Get-AzResource -TagName $tagname -TagValue $TagValue | where{$_.resourcegroupname -eq $rg}| Remove-AzResource -force

Upvotes: 0

Allen Walker
Allen Walker

Reputation: 73

Answer of @James does work, but I got prompt in PS Find-AzureRmResource will be deprecated in upcoming breaking release. (Probably I came back too late(2 months after he posted) and happen to meet this update.)

So I recommend to use Get-AzureRmResource with AzureRM 6.8.1.

Upvotes: 1

James Donovan
James Donovan

Reputation: 336

You could use the below changing the values and piping into the Remove-RmResource command, let me know how you get on!

I haven't tried this without the RG Name specified but cant see why not, just check the output before the pipe (ie. Find-AzureRmResource -TagName $tagname -TagValue $TagValue)

$rg= “ResourceGroupName”
$tagname = “TagName”
$TagValue =”TagValue”
Find-AzureRmResource -TagName $tagname -TagValue $TagValue | where{$_.resourcegroupname -eq $rg}| Remove-AzureRmResource -force

Upvotes: 4

Related Questions