{"record":{"id":"b18a76c9b34ba80b","repo":"hashicorp/terraform","slug":"can-t-delete-default-state-b18a76","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/consul/backend_state.go","lineNumber":62,"sourceCode":"\n\t\t\tenvs[key] = struct{}{}\n\t\t}\n\t}\n\n\tresult := make([]string, 1, len(envs)+1)\n\tresult[0] = backend.DefaultStateName\n\tfor k, _ := range envs {\n\t\tresult = append(result, k)\n\t}\n\n\treturn result, nil\n}\n\nfunc (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tif name == backend.DefaultStateName || name == \"\" {\n\t\treturn diags.Append(fmt.Errorf(\"can't delete default state\"))\n\t}\n\n\t// Determine the path of the data\n\tpath := b.statePath(name)\n\n\t// Delete it. We just delete it without any locking since\n\t// the DeleteState API is documented as such.\n\t_, err := b.client.KV().Delete(path, nil)\n\treturn diags.Append(err)\n}\n\nfunc (b *Backend) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\t// Determine the path of the data\n\tpath := b.statePath(name)\n\n\t// Determine whether to gzip or not\n\tgzip := b.gzip","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote-state/consul/backend_state.go#L44-L80","documentation":"DeleteWorkspace (backend_state.go:62) refuses to delete the default workspace (or an empty-name workspace) because the default state always exists and cannot be removed; deleting it would orphan the backend's baseline state.","triggerScenarios":"Calling workspace deletion on the Consul backend for the default workspace (e.g. `terraform workspace delete default`) or for an empty workspace name.","commonSituations":"Automation that loops over `terraform workspace list` and deletes each entry; an accidental `terraform workspace delete default`; a script that does not special-case default.","solutions":["Do not attempt to delete the default workspace; skip it in automation.","If you want to clear state, reset the default workspace's contents instead of deleting it.","Guard deletion scripts to skip the default workspace name."],"exampleFix":"# before\nfor ws in $(terraform workspace list | sed 's/[* ]//g'); do terraform workspace delete \"$ws\"; done\n# after\nfor ws in $(terraform workspace list | sed 's/[* ]//g'); do\n  [ \"$ws\" = \"default\" ] && continue\n  terraform workspace delete \"$ws\"\ndone","handlingStrategy":"validation","validationCode":"# never delete the default workspace\nws=\"default\"\n[ \"$ws\" != \"default\" ] && [ -n \"$ws\" ] \\\n  && terraform workspace delete \"$ws\" \\\n  || echo \"skip: cannot delete default workspace\"","typeGuard":"// guard deletion against the default/empty workspace name\nfunc isDefaultWorkspace(name string) bool {\n    return name == \"\" || name == \"default\"\n}","tryCatchPattern":null,"preventionTips":["Special-case 'default' in workspace cleanup scripts.","Reset default state contents rather than deleting the workspace.","Review automation that enumerates and deletes all workspaces."],"tags":["consul","workspaces","state","backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}