hashicorp/terraform · error
failed to write state: %w
Error message
failed to write state: %w
What it means
Returned during a local refresh-only operation when statemgr.WriteAndPersist fails to write and persist the refreshed state. This occurs after Refresh succeeded and produced newState, but persisting that state (to local file or remote backend) failed. The wrapped error is the persistence error.
Source
Thrown at internal/backend/local/backend_refresh.go:112
newState, refreshDiags = lr.Core.Refresh(lr.Config, lr.InputState, lr.PlanOpts)
log.Printf("[INFO] backend/local: refresh calling Refresh")
}()
if b.opWait(doneCh, stopCtx, cancelCtx, lr.Core, opState, op.View) {
return
}
// Write the resulting state to the running op
runningOp.State = newState
diags = diags.Append(refreshDiags)
if refreshDiags.HasErrors() {
op.ReportResult(runningOp, diags)
return
}
err := statemgr.WriteAndPersist(opState, newState, schemas)
if err != nil {
diags = diags.Append(fmt.Errorf("failed to write state: %w", err))
op.ReportResult(runningOp, diags)
return
}
// Show any remaining warnings before exiting
op.ReportResult(runningOp, diags)
}
View on GitHub (pinned to c9def3e214)
Solutions
- Check the wrapped error: fix disk space / write permissions for the state and backup paths.
- For remote backends, verify credentials and network, then re-run terraform refresh.
- Ensure no other Terraform run holds the state lock; force-unlock if a stale lock remains.
Defensive patterns
Strategy: retry
Try / catch
if err := statemgr.WriteAndPersist(opState, newState, schemas); err != nil {
// transient write failures (disk full, network) can be retried
return fmt.Errorf("failed to write state: %w", err)
} Prevention
- Ensure adequate disk space and write permissions on the state path.
- Keep remote backend credentials valid for the full operation duration.
- Use state locking to prevent concurrent writes that can corrupt persistence.
When it happens
Trigger: Calling terraform refresh (or refresh-equivalent) where the refresh succeeded but the state write/persist step fails — disk full, permission denied writing tfstate, remote backend write failure, or network error pushing state.
Common situations: Disk full or read-only filesystem on the state path; remote backend credentials expired mid-run; concurrent write/lock contention; backup file path unwritable.
Related errors
- failed checking for existing remote state: %s
- error deleting workspace %s: %w
- error loading state: %w
- Failed to open script '%s': %v
- Failed to retrieve workspace %s: %v
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/6faff8e797a2e816.
Report an issue: GitHub.