Noname
Noname

Reputation: 23

How to delete tags from multiple azure services?

So the question in the title : How to delete tags from multiple azure services ? It can be with PowerShell or GUI.Thanks)

Upvotes: 2

Views: 12198

Answers (6)

Harry Huy
Harry Huy

Reputation: 323

Today, I simply use azcli:

$Tag = 'tagName' # or 'tagName=tagValue'    

$Resources = az resource list --tag $Tag | ConvertFrom-Json

foreach ($resource in $Resources) {
    az tag update --resource-id $resource.id --operation 'delete' --tags $Tag
}

This script will only delete a specific tag upon tagged resources.

Upvotes: 6

spacebjorn
spacebjorn

Reputation: 91

Remove-AzureRmTag/Remove-AzTag only works on subscriptions if there's no resources/resource groups with the tag. Besides, AzureRM Powershell module is deprecated and should not be used anymore.

Below Is a script I've made to delete a tag or a dictionary of tags from a subscription.

You can delete a tag either with only the TagName which will delete all instnaces of the Tag on all resources that has the tag in a subscription. If you give the script a TagName and TagValue you can delete a specific tag with a specific value. If you pass an object to the script you will delete all key-value pairs listed in the object.

As you can see, you can now filter Get-AzResource based on a tag name or tag name + value. At the moment you cant only filter Get-AzResourceGroup with a tag object.

The upside of using the Update-AzTag method is that you only need a user with read access and Microsoft.Resources/tags/write on the scope you're operating on.

This has been tested with Az Powershell version 5.1

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]
    $SubscriptionName,

    [Parameter(ParameterSetName = "TagName", Mandatory)]
    [Parameter(ParameterSetName = "TagValue", Mandatory)]
    [string]
    $TagName,

    [Parameter(ParameterSetName = "TagValue", Mandatory)]
    [string]
    $TagValue,

    [Parameter(ParameterSetName = "TagObject", Mandatory)]
    [object]
    $Tag
)

Set-AzContext -SubscriptionName $SubscriptionName -ErrorAction:Stop

switch ($PSCmdlet.ParameterSetName) {
    "TagName" {
        Get-AzResourceGroup | Where-Object { $null -ne $_.Tags -and $_.Tags.ContainsKey("$TagName") } |  ForEach-Object {
            $_.Tags.Remove("$TagName")
            Update-AzTag -ResourceId $_.ResourceId -Tag $_.Tags -Operation Replace
        }

        Get-AzResource -TagName "$TagName" | ForEach-Object {
            $_.Tags.Remove("$TagName")
            Update-AzTag -ResourceId $_.Id -Tag $_.Tags -Operation Replace
        }
    }
    "TagValue" {
        Get-AzResourceGroup -Tag @{"$TagName" = "$TagValue" } | ForEach-Object {
            Update-AzTag -ResourceId $_.ResourceId -Tag @{"$TagName" = "$TagValue" } -Operation Delete
        }

        Get-AzResource -Tag @{"$TagName" = "$TagValue" } | ForEach-Object {
            Update-AzTag -ResourceId $_.Id -Tag @{"$TagName" = "$TagValue" } -Operation Delete
        }
    }
    "TagObject" {
        Get-AzResourceGroup -Tag $Tag | ForEach-Object {
            Update-AzTag -ResourceId $_.ResourceId -Tag $Tag -Operation Delete
        }

        Get-AzResource -Tag $Tag | ForEach-Object {
            Update-AzTag -ResourceId $_.Id -Tag $Tag -Operation Delete
        }
    }
}

Upvotes: 0

Ahmed IG
Ahmed IG

Reputation: 583

# That's a method that deletes the tag from your resource.
function Remove-TagsFromResource ($tagname, $tagvalue) {
    $resources = Get-AzResource -TagName $tagname -TagValue $tagvalue

    foreach ($resource in $resources) {
        Write-Host "Removing tag from " $resource.Name
        $deletedtag = @{"$tagname" = "$tagvalue" }
        Update-AzTag -ResourceId $resource.ResourceId -Tag $deletedtag -Operation Delete
        Write-Host "Tag removed from " $resource.Name -ForegroundColor Green
    }
}

# This is how you call the method
Remove-TagsFromResource -tagName "Key" -tagvalue "Value"

Try this one. Tested and works.

Upvotes: 0

Matt
Matt

Reputation: 216

Using Remove-AzureRmTag or now Remove-AzTag only removes the predefined tag and value, it doesn't remove that tag from a resource resources. This script will find all the resources tagged department:accounting across multiple subscriptions and remove that tag from the resources.

$subs = Get-AzSubscription

$tagname = "department"
$tagvalue = "accounting"

$subs | % {
    Set-AzContext $_
    $rs = Get-AzResource -TagName $tagname -TagValue $tagvalue
    $rs | % {
        $_.Tags.Remove($tagname)
        $_ | Set-AzResource -Force
    }  
}

Upvotes: 5

Chris Pietschmann
Chris Pietschmann

Reputation: 29905

The Azure Portal UI can be used to edit and delete Tags from Resources. It can also be scripted using the Azure PowerShell SDK or the Azure CLI command-line tools to automate the process.

The Remove-AzureRmTag PowerShell command can be used to remove a specific Tag from all resources. Here's an example of removing the Tag named "Department" from all resources:

Remove-AzureRmTag -Name "Department"

Further documentation for the Azure PowerShell SDK can be found here: https://learn.microsoft.com/en-us/powershell/module/azurerm.tags/remove-azurermtag?view=azurermps-6.13.0

Upvotes: -1

Piyush Zalani
Piyush Zalani

Reputation: 3934

You can do it using powershell:

Remove-AzureRmTag [-Name] <String> [[-Value] <String[]>] [-PassThru] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>] 

Upvotes: 0

Related Questions