Skip to content

Clearing a stale state lock

Symptom

An apply fails with Error acquiring the state lock, and the run waited five minutes first. The apply job passes -lock-timeout=5m on its plan and its apply, so a lock that is merely busy resolves on its own inside that window. A failure after it means the lock is held by nothing. PR plans and drift plans take no state lock, so they are not blocked by a stale one.

The known cause is an OOM-killed runner. The heaviest ALZ policy-library plans need more than the AVM runner defaults, and an undersized replica is killed mid-job, which leaves the lease on the state blob. See A runner is OOM-killed and leaves a stale lock for the runner sizing that prevents it.

Confirm nothing is running first

Never force-unlock a live run

Breaking the lock under a running apply corrupts state. Confirm the unit is idle before you touch it.

Do not confuse this with tfpr unlock: that command releases the engine's own cross-PR unit locks, not a Terraform state lease. Nothing in the engine clears a state lease, by design. Two checks, both of them:

  1. Open the repository's Actions tab and confirm no tfpr ops or drift run is in progress for this unit.
  2. Check the concurrency group. Plans and drift plans for a unit serialize on tfpr-<label>, where <label> is the unit path. The plan and apply jobs of one PR serialize on tfpr-pr-<pr-number>, so a plan pushed during an apply waits for it; its check rows read Waiting for apply #N on <sha> meanwhile. A queued run in either group means work is still pending, so wait for it rather than unlocking.

Find the lock ID

The error block in the job log and the PR comment names it:

Error: Error acquiring the state lock

Lock Info:
  ID:        6a1f0c92-1a4e-4b39-8d7c-2f0b6e4a91c3
  Path:      tfstate/landingzones/corp/corp-demo-dev/westeurope/network/terraform.tfstate
  Operation: OperationTypeApply
  Who:       runner@...
  Created:   ...

ID is the lease id you pass to force-unlock. Path is the state key, which is the unit's path under live/.

Force-unlock the unit

Run it locally against the unit, from a machine that can reach the state account and is signed in to Azure:

terragrunt --working-dir live/landingzones/corp/corp-demo-dev/westeurope/network \
  run -- force-unlock 6a1f0c92-1a4e-4b39-8d7c-2f0b6e4a91c3

The state account is default-deny and allows the runner's egress address, so this only works from an allowlisted address. If you cannot reach it, add your address to the state account firewall for the duration, or run from the runner's network.

Last resort: break the blob lease

If force-unlock cannot run, break the lease on the state blob directly:

az storage blob lease break \
  --auth-mode login \
  --account-name <state-account> \
  --container-name tfstate \
  --blob-name landingzones/corp/corp-demo-dev/westeurope/network/terraform.tfstate

This releases the lease but leaves Terraform's lock record behind, so the next run may still report a lock. Clear it by running force-unlock once the blob is writable again.

After either route, re-plan the unit through the PR and confirm the plan is what you expect before applying.