{"record":{"id":"6afd0191fabf11e1","repo":"hashicorp/terraform","slug":"error-migrating-the-workspace-q-from-the-previous","errorCode":null,"errorMessage":"Error migrating the workspace %q from the previous %q backend\nto the newly configured %q backend:\n    %s\n\nTerraform copies workspaces in alphabetical order. Any workspaces\nalphabetically earlier than this one have been copied. Any workspaces\nlater than this haven't been modified in the destination. No workspaces\nin the source state have been modified.\n\nPlease resolve the error above and run the initialization command again.\nThis will attempt to copy (with permission) all workspaces again.","messagePattern":"Error migrating the workspace %q from the previous %q backend\nto the newly configured %q backend:\n    %s\n\nTerraform copies workspaces in alphabetical order\\. Any workspaces\nalphabetically earlier than this one have been copied\\. Any workspaces\nlater than this haven't been modified in the destination\\. No workspaces\nin the source state have been modified\\.\n\nPlease resolve the error above and run the initialization command again\\.\nThis will attempt to copy \\(with permission\\) all workspaces again\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend_migrate.go","lineNumber":213,"sourceCode":"\tif wDiags.HasWarnings() {\n\t\tlog.Printf(\"[WARN] backendMigrateState_S_S: warning(s) returned when getting workspaces from source backend: %s\", wDiags.ErrWithWarnings())\n\t}\n\n\t// Sort the states so they're always copied alphabetically\n\tsort.Strings(sourceWorkspaces)\n\n\t// Go through each and migrate\n\tfor _, name := range sourceWorkspaces {\n\t\t// Copy the same names\n\t\topts.sourceWorkspace = name\n\t\topts.destinationWorkspace = name\n\n\t\t// Force it, we confirmed above\n\t\topts.force = true\n\n\t\t// Perform the migration\n\t\tif err := m.backendMigrateState_s_s(opts); err != nil {\n\t\t\treturn fmt.Errorf(strings.TrimSpace(\n\t\t\t\terrMigrateMulti), name, opts.SourceType, opts.DestinationType, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// Multi-state to single state.\nfunc (m *Meta) backendMigrateState_S_s(opts *backendMigrateOpts) error {\n\tlog.Printf(\"[INFO] backendMigrateState: destination backend type %q does not support named workspaces\", opts.DestinationType)\n\n\tcurrentWorkspace, err := m.Workspace()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tmigrate := opts.force\n\tif !migrate {","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta_backend_migrate.go#L195-L231","documentation":"Emitted during a multi-state-to-multi-state backend migration when copying a single workspace's state fails. The loop in backendMigrateState_S_S iterates over all source workspaces (sorted alphabetically) and calls backendMigrateState_s_s per workspace; if any one copy errors, it is wrapped with this message. The message communicates partial progress: earlier-named workspaces were copied, later-named ones were not touched, and the source is never modified.","triggerScenarios":"terraform init after changing backend config where BOTH old and new backends support named workspaces (multi-state), and the per-workspace single-to-single migration fails — e.g., destination state lock contention, destination write failure, or a per-workspace StateMgr error. Reached via the case at line 134-141 routing to backendMigrateState_S_S, then the loop at line 203-216.","commonSituations":"Switching from an S3 backend to a remote/cloud backend (or between remote backends) with multiple workspaces; destination bucket/bucket-key already in use by another state; stale state lock on a destination workspace; insufficient IAM permissions on a subset of workspaces; network blip mid-migration.","solutions":["Read the inner %s error to identify the failing workspace and root cause (lock, permissions, network).","Resolve the per-workspace issue (force-unlock, grant IAM/role access, fix bucket path), then re-run 'terraform init'.","Because earlier alphabetical workspaces already copied successfully, re-running init resumes from the same list — Terraform detects equal state with matching lineage and skips already-migrated workspaces.","If a destination workspace is corrupted, remove or repair only that destination workspace object before re-running.","For lock contention, run 'terraform force-unlock' on the destination workspace lock ID shown in the inner error."],"exampleFix":"// before: destination bucket had a stale lock on workspace 'staging'\n//   terraform init  -> Error migrating workspace \"staging\" ... state lock\n// fix:\n//   terraform force-unlock <LOCK_ID>   # on destination\n//   terraform init                     # resumes; staging copied this time","handlingStrategy":"retry","validationCode":"# Before migrating, verify each destination workspace is writable and unlocked.\nfor ws in $(terraform workspace list | tr -d '*'); do\n  terraform state list -state=<(terraform state pull) >/dev/null 2>&1 && echo \"$ws: source readable\"\ndone\n# Ensure no stale locks on destination:\n# (S3+DynamoDB) aws dynamodb scan --table-name locks --filter-expression \"LockID = :id\"","typeGuard":null,"tryCatchPattern":"# In CI automation around terraform init, retry transient multi-state failures.\ninit_state() {\n  terraform init -input=false -force-copy 2>&1 | tee /tmp/init.log\n  rc=${PIPESTATUS[0]}\n  if grep -q 'Error migrating the workspace' /tmp/init.log && [ $rc -ne 0 ]; then\n    return 1  # signal retry\n  fi\n  return 0\n}\nfor i in 1 2 3; do init_state && break || sleep $((i*5)); done","preventionTips":["Run 'terraform init' in an interactive mode first to surface per-workspace issues before automating.","Pre-flight: confirm destination backend has no stale locks (force-unlock before migrating).","Keep destination and source state sizes modest so per-workspace copies don't time out.","Use '-force-copy' in pipelines to avoid prompt-related partial failures."],"tags":["backend","state-migration","multi-state","terraform-init","partial-copy"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}