Files
opentf/website/docs/language/state/locking.mdx
2024-04-24 13:24:30 +02:00

40 lines
1.6 KiB
Plaintext

---
description: >-
OpenTofu stores state which caches the known state of the world the last time
OpenTofu ran.
---
# State Locking
If supported by your [backend](../../language/settings/backends/configuration.mdx), OpenTofu will lock your
state for all operations that could write state. This prevents
others from acquiring the lock and potentially corrupting your state.
State locking happens automatically on all operations that could write
state. You won't see any message that it is happening. If state locking fails,
OpenTofu will not continue. You can disable state locking for most commands
with the `-lock` flag but it is not recommended.
If acquiring the lock is taking longer than expected, OpenTofu will output
a status message. If OpenTofu doesn't output a message, state locking is
still occurring if your backend supports it.
Not all backends support locking. The
[documentation for each backend](../../language/settings/backends/configuration.mdx)
includes details on whether it supports locking or not.
## Force Unlock
OpenTofu has a [force-unlock command](../../cli/commands/force-unlock.mdx)
to manually unlock the state if unlocking failed.
**Be very careful with this command.** If you unlock the state when someone
else is holding the lock it could cause multiple writers. Force unlock should
only be used to unlock your own lock in the situation where automatic
unlocking failed.
To protect you, the `force-unlock` command requires a unique lock ID. OpenTofu
will output this lock ID if unlocking fails. This lock ID acts as a
[nonce](https://en.wikipedia.org/wiki/Cryptographic_nonce), ensuring
that locks and unlocks target the correct lock.