Error: Failed to persist state to backend: recover via errored.tfstate, not re-apply
Fixes Terraform's "Error: Failed to persist state to backend ... state has been written to errored.tfstate". Use when apply succeeds remotely but the state write fails (e.g. the state bucket was destroyed). Recover with terraform state push errored.tfstate after fixing the backend; do NOT re-apply blindly (forked state).
TL;DR
Your apply worked, but Terraform could not write the updated state back (the classic trigger: you just destroyed the bucket holding the state). Terraform saved the state locally as errored.tfstate instead of losing it. Fix the backend, then terraform state push errored.tfstate. Do not just re-apply: that forks the state.
The error
Error: Failed to persist state to backend
The error shown above has prevented Terraform from writing the updated state to the configured backend. To allow for recovery, the state has been written to the file "errored.tfstate" in the current working directory.
Running "terraform apply" again at this point will create a forked state, making it harder to recover.
To retry writing this state, use the following command:
terraform state push errored.tfstateSteps to fix
- Stop. Do not re-run
apply. Back uperrored.tfstatesomewhere safe right now.
- Expected: you have a copy of the post-apply state.
- Fix the backend: recreate the deleted bucket/table, restore network access, or correct the backend config.
- Expected: the backend is writable again.
- Push the saved state:
terraform state push errored.tfstate- Expected:
state pushsucceeds; remote state matches reality.
- Run
terraform planand confirm it shows no unexpected changes.
- Expected: plan is empty or matches intent; no fork.
When to use this
applyends withFailed to persist state to backendand anerrored.tfstatefile appears, usually after destroying the state bucket or losing backend connectivity mid-apply.
When NOT to use this
Failed to save statewithout theerrored.tfstaterecovery path is the same family; the push step is what is specific to this error. If the backend is fine and only the lock failed, address the lock.
Compatibility
- All Terraform versions; the
errored.tfstaterecovery flow is long-standing.
Root cause
Terraform writes state after the apply completes. If the backend disappears mid-run (destroyed bucket, revoked credentials, network drop), the write fails. Rather than lose the record of what was just done, Terraform dumps it locally and tells you exactly how to push it back.
Edge cases
- Never
terraform destroythe stack that owns the state bucket without migrating state first; this error is the predictable outcome. - If someone applied again before you pushed, you have a fork: compare serials, pick the authoritative copy, and push with
-forceonly deliberately. - Keep
errored.tfstateout of version control; it can contain secrets.