question

MADHUNAhmed-3798 avatar image
0 Votes"
MADHUNAhmed-3798 asked bharathn-msft edited

Delete resouce lock using Azure CLI task

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-supportednot-supported-azure
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MADHUNAhmed-3798 Can you please share the Azure service/product your issue related to? Are you using Azure Batch?

Azure DevOps is currently not supported on Microsoft Q&A.

The product group for Azure DevOps actively monitors questions over at
https://developercommunity.visualstudio.com/report?space=21&entry=problem

0 Votes 0 ·
MADHUNAhmed-3798 avatar image
0 Votes"
MADHUNAhmed-3798 answered

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"


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered MADHUNAhmed-3798 commented

Hello @MADHUNAhmed-3798 ,
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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried to run the suggested answer, but got an error, herer is the log:

E:\agent_1_work\r30\a>az lock list -g "rg_name" --query "[?type=='Microsoft.Authorization/locks'].id" --output tsv | xargs -n 1 -t az lock delete --id
az lock delete --id /subscriptions/7939c894-4b52-44ad-8aa1-27be16bfdfcc/resourcegroups/rg_name/providers/microsoft.storage/storageaccounts/asedevdtl9433/providers/Microsoft.Authorization/locks/DevTestLabs
ERROR: az lock delete: error: argument --ids: invalid ResourceId value: 'C:/Program Files/Git/subscriptions/7939c894-4b52-44ad-8aa1-27be16bfdfcc/resourcegroups/rg_name/providers/microsoft.storage/storageaccounts/asedevdtl9433/providers/Microsoft.Authorization/locks/DevTestLabs'
az lock delete --id Lock


ERROR: az lock delete: error: argument --ids: invalid ResourceId value: 'Lock
'
az lock delete --id /subscriptions/7939c894-4b52-44ad-8aa1-27be16bfdfcc/resourcegroups/rg_name/providers/Microsoft.KeyVault/vaults/SEDEVDTL7660/providers/Microsoft.Authorization/locks/DevTestLabs
ERROR: az lock delete: error: argument --ids: invalid ResourceId value: 'C:/Program Files/Git/subscriptions/7939c894-4b52-44ad-8aa1-27be16bfdfcc/resourcegroups/rg_name/providers/Microsoft.KeyVault/vaults/SEDEVDTL7660/providers/Microsoft.Authorization/locks/DevTestLabs'
az lock delete --id Lock


ERROR: az lock delete: error: argument --ids: invalid ResourceId value: 'Lock

0 Votes 0 ·

It seems that the problem is due to the naming of the lock that contains a space char, and have to be set in "" og {}.
This is an auto-generated lock name.

0 Votes 0 ·

If I run the only the first part of the command: az lock list -g "rg_name" --query "[?type=='Microsoft.Authorization/locks'].id" --output tsv

E:\agent_1_work\r30\a>az lock list -g "rg_name" --query "[?type=='Microsoft.Authorization/locks'].id" --output tsv
/subscriptions/7939c894-4b52-44ad-8aa1-27be16bfdfcc/resourcegroups/rg_name/providers/microsoft.storage/storageaccounts/asedevdtl9433/providers/Microsoft.Authorization/locks/DevTestLabs Lock
/subscriptions/7939c894-4b52-44ad-8aa1-27be16bfdfcc/resourcegroups/rg_name/providers/Microsoft.KeyVault/vaults/SEDEVDTL7660/providers/Microsoft.Authorization/locks/DevTestLabs Lock

0 Votes 0 ·