A Terraform apply crashed halfway through and left the state lock held. A teammate wants the lock cleared and a new apply started immediately. What is the safe handling of that lock?
Select an answer to reveal the explanation.
Short Explanation
A crashed job left the workshop door bolted, and breaking the bolt while someone is still inside wrecks the work. Clear the Terraform state lock only after you know no apply is still running. Starting another apply on top of a live lock is how your state gets corrupted.
Full Explanation
Terraform takes a state lock so two applies cannot write the state at the same time. A crashed apply can exit without releasing that lock. Force-unlock is safe only after you have confirmed the crashed process is gone and no other apply is holding the lock. Clearing it while a live apply still runs lets a second writer interleave API calls and state updates. Starting another apply immediately does not clear a held lock, and forcing the new run through overlaps whatever the crashed run already changed. Deleting the state object drops the inventory of real resources. The next apply no longer knows what it manages and can plan creates or destroys that do not match the account. Turning locking off removes the protection that stopped the overlap. Caveat: the crashed run may have finished some changes and not recorded them, so after the unlock the next action is a plan you read, not an unattended apply of the old plan. The lock id in the error is the one you clear. Operational check: no apply process should be running, the lock should be gone, and a fresh plan should be reviewed before anything is applied.