{"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":"warning","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote-state/inmem/backend.go#L101-L137","documentation":"The in-memory backend refuses to delete its default workspace. DeleteWorkspace() checks the requested name against backend.DefaultStateName (\"default\") and the empty string, and returns this error to protect the always-present default state from removal.","triggerScenarios":"Invoking `terraform workspace delete default` (or deleting an empty-named workspace) while using the `inmem` backend, which calls DeleteWorkspace at inmem/backend.go:114-124.","commonSituations":"CI/test scripts that programmatically clean up all workspaces without skipping 'default'; a test helper that iterates Workspaces() and deletes each one including the default.","solutions":["Skip the workspace named 'default' (and the empty string) in any cleanup loop that deletes workspaces.","Delete or recreate non-default workspaces only; the default workspace is managed by the backend and cannot be removed.","To reset all in-memory state in tests, call inmem.Reset() directly rather than deleting individual workspaces."],"exampleFix":"# before - deletes every workspace including default\nfor ws in $(terraform workspace list); do terraform workspace delete \"$ws\"; done\n\n# after - skip the protected default workspace\nfor ws in $(terraform workspace list); do\n  [ \"$ws\" = \"default\" ] && continue\n  terraform workspace delete \"$ws\"\ndone","handlingStrategy":"validation","validationCode":"// Guard any workspace cleanup loop against the protected default name\nfunc deleteAllNonDefault(b backend.Backend, names []string) error {\n    for _, n := range names {\n        if n == \"default\" || n == \"\" {\n            continue\n        }\n        if diags := b.DeleteWorkspace(n, false); diags.HasErrors() {\n            return diags.Err()\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always skip \"default\" (and empty) names in workspace deletion loops.","Call inmem.Reset() in test teardown instead of deleting workspaces one by one.","Treat the default workspace as undeletable by design."],"tags":["inmem","remote-state","workspace","terraform-backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}