Safeguard
Developer Tools•

What is Terraform state locking and when is force-unlock safe?

State locking stops two runs from writing state at once. Learn what force-unlock does, why it needs a lock ID, and the one case where using it is safe.

Safeguard Security Team
3 min read

State locking stops two Terraform operations from writing the same state at once. terraform force-unlock removes a lock manually, and HashiCorp says to use it only to release your own lock after automatic unlocking failed, because unlocking someone else's lock can cause multiple writers.

What locking does

If your backend supports it, Terraform locks the state for every operation that could write it. This prevents others from acquiring the lock and possibly corrupting the state. It happens automatically with no message, unless acquiring the lock takes longer than expected.

Not every backend supports locking. The backend documentation says which ones do, so check before you rely on it.

Why it matters for security

A corrupted or half written state can lead to a plan that destroys or recreates resources. In a shared pipeline, two simultaneous applies are also a way for a stale plan to overwrite a newer change. Locking is a control against accidental damage, and it supports change management.

The force-unlock command

If a run crashes, the lock can remain. Terraform prints a unique lock ID when unlocking fails. The force-unlock command requires that ID, which acts as a nonce so that locks and unlocks target the correct lock.

HashiCorp is blunt: be very careful. Unlock only your own lock after a failed automatic unlock.

Decision steps

  1. Find out who holds the lock, from the error message and your pipeline history.
  2. Confirm that run has really stopped, not just slowed down.
  3. If it is your run and it is dead, run force-unlock with the lock ID.
  4. If it belongs to someone else, ask them first.
  5. After unlocking, run a fresh plan before any apply.

Locks in team practice

Pair locking with a single place where applies run. If developers can apply from laptops while a pipeline also applies, locking helps, but it cannot stop a plan made against old code from being applied afterward. Review rules that route all applies through one pipeline do more than any flag.

The -lock=false flag

You can disable locking for most commands with -lock=false, but the documentation recommends against it. In a pipeline, forbid the flag in review rules.

Quick comparison

ActionRiskWhen
WaitLowA run is still in progress
force-unlock own lockLow to mediumYour run died
force-unlock someone else'sHighAvoid unless you have confirmed no run is active
-lock=falseHighAvoid

Quick check

Does every backend lock state?

No. Check the documentation for your backend.

What is the lock ID for?

It makes sure force-unlock targets the correct lock.

Should I plan again after force-unlock?

Yes. Run a fresh plan before applying, since state may have changed.

Safeguard offers SBOM and dependency scanning and policy gates that can block a pull request on findings, and you can try a free scan at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.