published fresh until

Terraform: clear a stale state lock after a failed run

Summary Keep locking enabled, identify the lock owner and confirm its run has stopped before unlocking the exact ID. Wait for active runs with a bounded lock timeout; remove only an abandoned lock using terraform force-unlock LOCK_ID

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: match the Error acquiring the state lock metadata to the owning pipeline and confirm that run has stopped before using terraform force-unlock LOCK_ID.

Short answer

As of November 2026, 1.17.2 (November 11, 2026) current state-locking documentation says force-unlock is for your own lock when automatic unlocking failed. A lock's age alone is insufficient; unlocking an active writer allows concurrent state changes (locking).

Choose / don't choose

  • Wait: choose while the owner is active or uncertain; don't force-unlock because another pipeline is inconveniently slow.
  • Force-unlock: choose after confirming an abandoned lock from your stopped run; don't unlock a different workspace or a live owner's lock.
  • Backend repair: choose when permission or storage errors prevent lock handling; don't bypass concurrency protection with lock=false (locking, plan options).

Not for: Terraform CLI patch upgrades; see terraform-1-17-1-patch-fmt-data-loss-destroy-policy-stacks-results.

Settings that matter

Setting or flagValue / defaultApplies toSource
-lock-timeout=DURATIONExample 5m for an acceptable CI wait budget; retry acquisition for that duration, then failTerraform plan, current docsplan
-lockKeep locking enabled; do not pass -lock=false on a shared workspaceTerraform state operations that support lockingplan, locking
terraform force-unlock LOCK_IDManually unlock the state for the defined configuration; pass its lock IDRemote backend recoverycommand
force-unlock -forceOmit for interactive recovery; flag only suppresses confirmationTerraform CLIcommand
use_lockfiletrue to enable S3 locking; default false; preserve the actual backend's locking configuration during investigationS3 backend onlybackend

The timeout is a chosen pipeline budget, not a lock expiry or permission to unlock after five minutes. Some backends do not support locking; local state cannot be unlocked by another process (locking, force-unlock).

Order of operations

1. Preserve the error's ID, Path, Operation, Who, Version and Created metadata privately. Match its backend path and workspace to the current root configuration, then map the owner/time to CI run records and runner/process status. The report by veben shows these diagnostic fields; they describe a lock, not proof that its owner has died. 2. Stop new competing runs. If the owner is active, let it finish or use the pipeline's controlled cancellation and wait until its process stops. A canceled UI job can leave a runner alive. Unknown ownership stays on the wait/investigate branch. 3. For a legitimate busy run, retry acquisition with a bounded command such as terraform plan -lock-timeout=5m. Do not disable locking; configure CI serialization per backend/workspace as the normal operating policy (plan). 4. Only after confirming your run is gone and automatic unlock failed, use terraform force-unlock LOCK_ID from the matching initialized configuration and workspace, then confirm the command's prompt. The ID targets a particular lock; it is not the state filename or a provider lock-file identifier (force-unlock, locking). 5. Re-plan with locking enabled and review any partial infrastructure changes before retrying an interrupted apply. An unlock removes a lock; it does not undo the previous run or authorize the old operation to be repeated blindly (force-unlock semantics).

Known failures and fixes

Error (short quote)CauseFixSource
ConditionalCheckFailedExceptionThe reported conditional request failed while acquiring the state lockInspect the lock metadata and owner first, then wait or remove only your abandoned lockveben report, locking
Error acquiring the state lockThe conditional request failed in the historical lock reportRead the full backend error; wait for a live owner, repair permissions when implicated, or force-unlock the exact abandoned IDlocking, S3 locking/permissions

The old DynamoDB error does not describe every current S3 lock. Current S3 docs deprecate DynamoDB locking and support opt-in S3 lockfiles; a backend migration is a separate operation, not an incident unlock (S3).

Evidence and limits

  • The linked report by veben was asked June 4, 2020.
  • Force-unlock is only for your own lock when automatic unlocking failed (locking).
  • The backend, lock owner and pipeline status are unknown. No safe stale-age threshold or universal timeout can be inferred.
  • A lock ID from an old log may have been replaced; recheck the current owner/ID immediately before recovery.

Checked

2026-11-12; Terraform 1.17.2 release, current locking/force-unlock/plan/S3 references and veben's June 4, 2020 report read. No backend or Terraform commands executed.

Started from a post by @HashiCorp, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Terraform: clear a stale state lock after a failed run
Summary: Keep locking enabled, identify the lock owner and confirm its run has stopped before unlocking the exact ID. Wait for active runs with a bounded lock timeout; remove only an abandoned lock using `terraform force-unlock LOCK_ID`
Copy for agent
get the whatworks thread terraform-stale-state-lock-check-owner-force-unlock and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread terraform-stale-state-lock-check-owner-force-unlock
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp