{"record":{"id":"687ecae5cc54cb7d","repo":"hashicorp/terraform","slug":"can-t-delete-default-state-687eca","errorCode":null,"errorMessage":"can't delete default state","messagePattern":"can't delete default state","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/inmem/backend.go","lineNumber":119,"sourceCode":"\tstates.Lock()\n\tdefer states.Unlock()\n\n\tvar workspaces []string\n\n\tfor s := range states.m {\n\t\tworkspaces = append(workspaces, s)\n\t}\n\n\tsort.Strings(workspaces)\n\treturn workspaces, nil\n}\n\nfunc (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {\n\tstates.Lock()\n\tdefer states.Unlock()\n\n\tif name == backend.DefaultStateName || name == \"\" {\n\t\treturn tfdiags.Diagnostics{}.Append(fmt.Errorf(\"can't delete default state\"))\n\t}\n\n\tdelete(states.m, name)\n\treturn nil\n}\n\nfunc (b *Backend) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\tstates.Lock()\n\tdefer states.Unlock()\n\n\ts := states.m[name]\n\tif s == nil {\n\t\ts = &remote.State{\n\t\t\tClient: &RemoteClient{\n\t\t\t\tName: name,\n\t\t\t},","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote-state/inmem/backend.go#L101-L137","documentation":"Thrown by the in-memory backend's DeleteWorkspace when the workspace name equals backend.DefaultStateName (\"default\") or is empty (backend.go:118-121). The default workspace is protected and cannot be deleted; only non-default workspaces can be removed from the inmem store.","triggerScenarios":"Calling 'terraform workspace delete default' or programmatically invoking DeleteWorkspace(\"default\") / DeleteWorkspace(\"\") against the inmem backend.","commonSituations":"A script that loops over all workspaces and deletes them hits the default workspace; attempting to clean up by deleting the default workspace instead of switching off it; tests that delete workspaces indiscriminately.","solutions":["Switch to a non-default workspace first, then delete that workspace — never delete 'default'.","In cleanup scripts, skip name == \"default\" before calling DeleteWorkspace.","To reset default state, use 'terraform destroy' or overwrite state rather than deleting the workspace."],"exampleFix":"// before\nfor _, ws := range workspaces {\n    b.DeleteWorkspace(ws, false)\n}\n// after\nfor _, ws := range workspaces {\n    if ws == \"default\" || ws == \"\" {\n        continue\n    }\n    b.DeleteWorkspace(ws, false)\n}","handlingStrategy":"validation","validationCode":"// Never delete the default workspace — guard before calling:\n// if name == backend.DefaultStateName || name == \"\" {\n//   return errors.New(\"refusing to delete default workspace\")\n// }\n// b.DeleteWorkspace(name, force)","typeGuard":null,"tryCatchPattern":"// err := b.DeleteWorkspace(name, force)\n// if err != nil && strings.Contains(err.Error(), \"can't delete default state\") {\n//   // expected for the default workspace; skip\n// }","preventionTips":["Always skip name == \"default\" in workspace-cleanup loops.","Use 'terraform workspace select default' before deleting others.","Reset default state with terraform destroy instead of workspace deletion."],"tags":["inmem-backend","workspace","default-state"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}