Remove locks from Azure resources

Remove locks from azure resources to be able to change or delete them

In my previous blog post Lock Azure resources to prevent accidental deletion, I showed how to add a lock to a resource with an ARM template to protect it from accidental deletion. When you want to delete the resource, you first need to remove the lock. A lock cannot be removed with an ARM template. To remove the lock you can use:

  • Powershell
  • Rest API
  • Portal

When deploying with ARM templates, the deployment will not remove locks. This will protect your resource from accidental deletion in a Infrastructure as Code scenario when deploying from VSTS.

Only the roles Owner and User Access Administrator can delete the locks on the resources. After the lock is removed the resource can be delete as any other resource.

Removing a locks with Powershell
Powershell has the following cmdlets to manage locks:

Only the roles Owner and User Access Administrator can manage the locks on the resources.

The New-AzureRmResourceLock sets a new lock to a resource, the Get-AzureRmResourceLock cmdlets shows all the locks in you subscription, the Set-AzureRmResourceLock enables you to change locks and the Remove-AzureRmResourceLock will remove the locks. The following powershell command will remove all the locks within the specified resourcegroup:

$rg-name = "rgwithlocksname"
Get-AzureRmResourceLock | where ResourceGroupName -eq "$rg-name" | Remove-AzureRmResourceLock -Force

Removing locks with the Rest-API
Locks can also be managed with the Rest-API: Here you can see the API: Microsoft documentation management locks Rest-API

Removing locks from the Portal
Next you can also remove the locks from the portal. To do this, go to the resource and open the lock tab in the settings. If you delete a resource group with a locked resource, the portal UI will give you an error and no resources are deleted.

When deploying resources with ARM template, locks can be helpful to protect your critical resource from accidental deletion. They can’t be deleted with an ARM template (even if it deploys in complete mode). If your contributors Users in the portal do not have the rights to manage locks, only the subscription owner will be able to delete the locked resources.

One thought on “Remove locks from Azure resources”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: