Delete resouce lock using Azure CLI task

MADHUN Ahmed 241 Reputation points
2021-08-11T11:43:11.177+00:00

I am using the Azure CLI task in Azure Devops to run a resource lock deletion command against Azure.

The following command is getting the list of locks that i have in my resource group

az lock list -g "RG_Name" --query "[?type=='"Microsoft.Authorization/locks"'].id"

However, I am not able to further use | xargs, to delete each of the found locks by the previous command. Any suggestion to write it in the correct syntax ?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,162 questions
{count} votes

Accepted answer
  1. MADHUN Ahmed 241 Reputation points
    2021-09-24T07:59:03.493+00:00

    I managed to apply a workaround by doing it in the following code for each lock:

     for /f "delims=" %%i in ('az resource list -g RG_SE_Polisen_Dev --query "[?type=='Microsoft.KeyVault/vaults'].name" --output tsv') do set output=%%i
    
     az lock delete --ids "/subscriptions/{SubID}/resourcegroups/{Rg_name}/providers/Microsoft.KeyVault/vaults/%output%/providers/Microsoft.Authorization/locks/DevTestLabs Lock"
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. shiva patpi 13,141 Reputation points Microsoft Employee
    2021-08-30T18:54:03.607+00:00

    Hello @MADHUN Ahmed ,
    Thanks for your query !
    Please try the below command ( I just tried from my end and it is working)

    az lock list -g rgname --query "[?type=='Microsoft.Authorization/locks'].id" --output tsv | xargs -n 1 -t az lock delete --id
    example output
    az lock delete --id /subscriptions/subid/resourceGroups/akswithudr/providers/Microsoft.Authorization/locks/s1
    az lock delete --id /subscriptions/subid/resourceGroups/akswithudr/providers/Microsoft.Authorization/locks/s2
    az lock delete --id /subscriptions/subid/resourceGroups/akswithudr/providers/Microsoft.Authorization/locks/s3

    Let us know if that helps !

    Please make sure to "UpVote & Accept the answer" , so that it will help the community out there who are looking for similar queries

    Regards,
    Shiva