{"record":{"id":"8eb555a29f461e66","repo":"hashicorp/terraform","slug":"can-t-delete-default-state-8eb555","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/kubernetes/backend_state.go","lineNumber":73,"sourceCode":"\t\t// Make sure it isn't default and the key matches\n\t\tif ws != backend.DefaultStateName && key == b.nameSuffix {\n\t\t\tm[ws] = struct{}{}\n\t\t}\n\t}\n\n\tstates := []string{backend.DefaultStateName}\n\tfor k := range m {\n\t\tstates = append(states, k)\n\t}\n\n\tsort.Strings(states[1:])\n\treturn states, diags\n}\n\nfunc (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\tif name == backend.DefaultStateName || name == \"\" {\n\t\treturn diags.Append(fmt.Errorf(\"can't delete default state\"))\n\t}\n\n\tclient, err := b.remoteClient(name)\n\tif err != nil {\n\t\treturn diags.Append(err)\n\t}\n\n\treturn diags.Append(client.Delete())\n}\n\nfunc (b *Backend) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\tc, err := b.remoteClient(name)\n\tif err != nil {\n\t\treturn nil, diags.Append(err)\n\t}\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote-state/kubernetes/backend_state.go#L55-L91","documentation":"The Kubernetes backend refuses to delete its default workspace. DeleteWorkspace() rejects the name 'default' and the empty string at backend_state.go:72-74, mirroring other backends, because the default workspace is always present and cannot be removed.","triggerScenarios":"Running `terraform workspace delete default` while using the kubernetes backend, which calls DeleteWorkspace at backend_state.go:70-82.","commonSituations":"Workspace cleanup automation that enumerates and deletes all workspaces without excluding 'default'; CI teardown scripts that iterate `terraform workspace list`.","solutions":["Exclude the 'default' workspace from any deletion loop.","Delete only named non-default workspaces; the default is immutable in this respect.","If you need to clear default state, use `terraform state` subcommands or delete the underlying Secret manually (risk: orphaned lease)."],"exampleFix":"# before - attempts to delete the protected default workspace\nterraform workspace delete default\n\n# after - delete a non-default workspace instead\nterraform workspace select default\nterraform workspace delete my-feature-env","handlingStrategy":"validation","validationCode":"// Skip the protected default workspace in any k8s backend deletion routine\nfor _, ws := range workspaces {\n    if ws == backend.DefaultStateName || ws == \"\" {\n        continue\n    }\n    _ = b.DeleteWorkspace(ws, false)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never target 'default' in workspace deletion automation.","Document that the default workspace is immutable for deletion.","Clear default state via state subcommands rather than workspace deletion."],"tags":["kubernetes","remote-state","workspace","terraform-backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}