{"record":{"id":"6faff8e797a2e816","repo":"hashicorp/terraform","slug":"failed-to-write-state-w","errorCode":null,"errorMessage":"failed to write state: %w","messagePattern":"failed to write state: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/local/backend_refresh.go","lineNumber":112,"sourceCode":"\t\tnewState, refreshDiags = lr.Core.Refresh(lr.Config, lr.InputState, lr.PlanOpts)\n\t\tlog.Printf(\"[INFO] backend/local: refresh calling Refresh\")\n\t}()\n\n\tif b.opWait(doneCh, stopCtx, cancelCtx, lr.Core, opState, op.View) {\n\t\treturn\n\t}\n\n\t// Write the resulting state to the running op\n\trunningOp.State = newState\n\tdiags = diags.Append(refreshDiags)\n\tif refreshDiags.HasErrors() {\n\t\top.ReportResult(runningOp, diags)\n\t\treturn\n\t}\n\n\terr := statemgr.WriteAndPersist(opState, newState, schemas)\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"failed to write state: %w\", err))\n\t\top.ReportResult(runningOp, diags)\n\t\treturn\n\t}\n\n\t// Show any remaining warnings before exiting\n\top.ReportResult(runningOp, diags)\n}\n","sourceCodeStart":94,"sourceCodeEnd":120,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/local/backend_refresh.go#L94-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"Disk full or read-only filesystem on the state path; remote backend credentials expired mid-run; concurrent write/lock contention; backup file path unwritable.","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."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := statemgr.WriteAndPersist(opState, newState, schemas); err != nil {\n    // transient write failures (disk full, network) can be retried\n    return fmt.Errorf(\"failed to write state: %w\", err)\n}","preventionTips":["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."],"tags":["state","refresh","persistence","filesystem","terraform"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}