OnCallReadyStart learning

Azure Lesson 32.15 · Azure IV: Governance, Storage & Backup · 15 min read

Resource locks: the last guard before delete

In plain words

A lock is a padlock on a cupboard that even the building manager has to unlock on purpose before throwing the cupboard away. A "do not delete" padlock still lets people use and rearrange what is inside; a "read only" padlock means nobody may change anything at all - not even to hand out a new key.

Padlocks on a room apply to every cupboard in it.

Resource locks: the last guard before delete

RBAC decides who may delete. Policy decides what may exist. Neither protects the production resource group from the Owner who ran az group delete in the wrong subscription. A resource lock does: it overrides every permission, including Owner, until someone with the right role removes it on purpose. On-call meets locks from both sides: they save the database, and they make a cleanup, a deploy or a backup fail at 2 am with ScopeLocked.

Need to know:

Level (CLI)PortalBlocks
CanNotDeleteDeletedeleting the resource (or anything under the scope); reads and changes still work
ReadOnlyRead-onlyevery write and delete on the control plane: updates, tags, role assignments, and POST actions (list keys, start/stop VM...)
  • A lock at a subscription, resource group or resource is inherited by everything below it. When several apply, the most restrictive wins.
  • Locks act on the control plane (management.azure.com, Azure Resource Manager). They do not act on data inside the resource: a ReadOnly storage account still accepts blob uploads from a user with a data role.
  • Creating and deleting locks needs Microsoft.Authorization/locks/*: Owner and User Access Administrator have it, Contributor does not. That is the point: the people who deploy can't quietly remove the guard.

CanNotDelete

$ az group create -n rg-try-locks -l westeurope -o none
$ az storage account create -n sttrylock01 -g rg-try-locks -o none
$ az lock create -n keep-rg -g rg-try-locks --lock-type CanNotDelete --notes "Prod data - ask #platform before removing" -o table
Level         Name     Notes                                      ResourceGroup
------------  -------  -----------------------------------------  -------------
CanNotDelete  keep-rg  Prod data - ask #platform before removing  rg-try-locks
$ az storage account update -n sttrylock01 -g rg-try-locks --tags owner=sre --query tags
{
  "owner": "sre"
}
$ az storage account delete -n sttrylock01 -g rg-try-locks --yes
ERROR: (ScopeLocked) The scope '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Storage/storageAccounts/sttrylock01' cannot perform delete operation because following scope(s) are locked: '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks'. Please remove the lock and try again.
Code: ScopeLocked
Message: The scope '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Storage/storageAccounts/sttrylock01' cannot perform delete operation because following scope(s) are locked: '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks'. Please remove the lock and try again.
$ az group delete -n rg-try-locks --yes
ERROR: (ScopeLocked) The scope '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks' cannot perform delete operation because following scope(s) are locked: '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks'. Please remove the lock and try again.
Code: ScopeLocked
Message: The scope '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks' cannot perform delete operation because following scope(s) are locked: '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks'. Please remove the lock and try again.
  • The update works: CanNotDelete only stops deletes.
  • The error names two scopes: the one you tried to delete and the one holding the lock. Here they are both the group. When the lock sits higher (a subscription lock someone forgot), the second scope is where you look.
  • --notes is the message for whoever hits the lock. Put the reason and who to ask in it.

A lock on one resource, by name and type:

$ az lock create -n keep-sa -g rg-try-locks --resource sttrylock01 --resource-type Microsoft.Storage/storageAccounts --lock-type CanNotDelete -o none
$ az lock list -g rg-try-locks --query "[].{name:name, level:level, scope:id}" -o table
Name     Level         Scope
-------  ------------  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
keep-rg  CanNotDelete  /subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Authorization/locks/keep-rg
keep-sa  CanNotDelete  /subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Storage/storageAccounts/sttrylock01/providers/Microsoft.Authorization/locks/keep-sa

az lock list -g shows the locks on the group and on the resources in it. The id says where each lock lives (everything before /providers/Microsoft.Authorization/locks/). Removing the group's lock is not enough to delete the account: its own lock still holds.

ReadOnly, and the surprises it causes

ReadOnly sounds like "nobody can break it". In practice it breaks things that only look like reads. First a data role for you (role assignments are writes, so after the lock this would be refused):

$ az role assignment create --assignee [email protected] --role "Storage Blob Data Contributor" --scope $(az storage account show -n sttrylock01 -g rg-try-locks --query id -o tsv) -o none
$ az storage container create --account-name sttrylock01 -n reports --auth-mode login -o none
$ az lock create -n freeze -g rg-try-locks --lock-type ReadOnly -o none
$ echo q3 > q3.csv
$ az storage blob upload --account-name sttrylock01 -c reports -n q3.csv -f q3.csv --auth-mode login -o none 2>/dev/null && echo uploaded
uploaded
$ az storage blob list --account-name sttrylock01 -c reports --query "[].name" -o tsv 2>&1 | head -1
ERROR: (ScopeLocked) The scope '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Storage/storageAccounts/sttrylock01' cannot perform write operation because following scope(s) are locked: '/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks'. Please remove the lock and try again.
  • The upload with --auth-mode login went through: the data plane does not see locks.
  • The list without --auth-mode login failed: with no credentials given, the CLI fetches the account key, and listKeys is a POST action, which ReadOnly blocks. Every tool that authenticates with account keys (scripts, old SDKs, the portal's storage browser in key mode, AzCopy with a key-signed SAS made on the fly) breaks the same way.
  • Other ReadOnly casualties: a VM can't be started, stopped or resized; an App Service plan can't scale; a Kubernetes cluster can't change node pools; NSG rules can't be added during an incident. Use ReadOnly for things that really should never change. CanNotDelete is the everyday lock.

Locks that break automation

  • Deployment history: a CanNotDelete lock on a resource group stops Azure Resource Manager from pruning old entries of the group's deployment history (it keeps up to 800). When the history is full, every new deployment to the group fails.
  • Backup: Azure Backup keeps instant-restore snapshots in a restore point collection, in a resource group it manages. A lock on that group stops it from cleaning old ones, and backups fail once the collection is full.
  • Cleanup jobs: a nightly job that deletes env=ephemeral groups fails on the one group someone locked "just for the demo". The job's log says ScopeLocked, the activity log says who added the lock and when.
$ az monitor activity-log list -g rg-try-locks --offset 1h --query "[?contains(operationName.value, 'locks/write')].{when:eventTimestamp, who:caller, lock:resourceId}" -o table
When                              Who                               Lock
--------------------------------  --------------------------------  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2026-09-22T20:00:05.250000+00:00  [email protected]  /subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Authorization/locks/freeze
2026-09-22T20:00:04.850000+00:00  [email protected]  /subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Storage/storageAccounts/sttrylock01/providers/Microsoft.Authorization/locks/keep-sa
2026-09-22T20:00:04.450000+00:00  [email protected]  /subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-try-locks/providers/Microsoft.Authorization/locks/keep-rg

Removing a lock

$ az lock delete -n freeze -g rg-try-locks
$ az lock delete -n keep-sa -g rg-try-locks --resource sttrylock01 --resource-type Microsoft.Storage/storageAccounts
$ az lock list -g rg-try-locks -o table
Level         Name     ResourceGroup  Notes
------------  -------  -------------  -----------------------------------------
CanNotDelete  keep-rg  rg-try-locks   Prod data - ask #platform before removing

Before deleting someone else's lock, read its notes and find out why it is there (the activity log has the creator). Put it back after the change. In infrastructure as code, declare the lock with the resource (Microsoft.Authorization/locks in Bicep, azurerm_management_lock in Terraform), so it is reviewed and can't silently disappear.

On the exam (AZ-104 1.3.2): the two levels and what each blocks, inheritance and "most restrictive wins", who can manage locks (Owner, User Access Administrator; not Contributor), locks act on the control plane only, ReadOnly blocking listKeys and VM operations.

In an interview: "What is the difference between a CanNotDelete and a ReadOnly lock, and which one do you put on production?" - "CanNotDelete lets authorised users read and change the resource but not delete it. ReadOnly blocks every control-plane write, including POST actions like listing storage keys or starting a VM. Both are inherited and override RBAC, even Owner. On production groups I use CanNotDelete. ReadOnly breaks normal operations, so only resources that must never change get it."

What you can now do

  • Lock a group or a single resource with a note, and find which scope holds a lock.
  • Predict what ReadOnly breaks: updates, role assignments, listKeys, VM operations.
  • Recognise ScopeLocked in a failing script, find who locked it, and remove a lock deliberately.

Why it helps

Locks are the last guard against the wrong az group delete in the wrong subscription, and they are also a common cause of failing cleanups, deployments and backups. Knowing what each level blocks - and that ReadOnly blocks things that look like reads, such as listing storage keys - saves hours during an incident.

On the exam: who can manage locks, what is inherited, and what ReadOnly breaks.

Commands in this lesson

az echo

FAQ

Can an Owner delete a locked resource?

Not while the lock is there: locks override RBAC, including Owner. An Owner (or User Access Administrator) can remove the lock first and then delete. A Contributor cannot remove locks at all, which is the point.

Why does my script fail on a storage account with a ReadOnly lock when it only lists blobs?

Without credentials the CLI fetches the account key, and listKeys is a POST action, which ReadOnly blocks. Use --auth-mode login with a data role (the data plane ignores locks), or remove the lock if it should not be there.

Do locks stop data changes inside a storage account?

No. Locks act on the control plane (Azure Resource Manager). With a data role or a SAS you can still upload and delete blobs in a locked account. Protect data with soft delete, versioning and backups, not locks.

Where do I find who added a lock?

In the activity log: the operation Microsoft.Authorization/locks/write, with the caller and the time (kept 90 days). Read the lock's notes too; good notes say why and who to ask. Then ask that person before removing it, and put it back after the change if it is still needed.

Can a lock break automation?

Yes. A CanNotDelete lock on a resource group stops Resource Manager from pruning the deployment history (800 entries), and a lock on Azure Backup's restore point collection group stops it cleaning old snapshots. Cleanup jobs fail with ScopeLocked on locked groups.

In an interview Mid

What is the difference between a CanNotDelete and a ReadOnly lock, and which one do you put on production?

CanNotDelete lets authorised users read and change the resource but not delete it. ReadOnly blocks every control-plane write, including POST actions like listing storage keys or starting a VM. Both are inherited and override RBAC, even Owner, and only Owner or User Access Administrator can manage them. On production groups I use CanNotDelete; ReadOnly breaks normal operations, so only resources that must never change get it.

Also asked: Who can create and delete resource locks? · Do resource locks protect the data in a storage account? · A cleanup job fails with ScopeLocked. How do you find the lock?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.