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) | Portal | Blocks |
|---|---|---|
CanNotDelete | Delete | deleting the resource (or anything under the scope); reads and changes still work |
ReadOnly | Read-only | every 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.
--notesis 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 loginwent through: the data plane does not see locks. - The list without
--auth-mode loginfailed: 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=ephemeralgroups 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.