{"record":{"id":"381ab1fa23ec3251","repo":"hashicorp/terraform","slug":"can-t-delete-default-state-381ab1","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/oss/backend_state.go","lineNumber":109,"sourceCode":"\t\t\t\toptions = append(options, oss.Marker(lastObj))\n\t\t\t}\n\t\t\tresp, err = bucket.ListObjects(options...)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, diags.Append(err)\n\t\t\t}\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t}\n\tsort.Strings(result[1:])\n\treturn result, diags\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\tclient, err := b.remoteClient(name)\n\tif err != nil {\n\t\treturn diags.Append(err)\n\t}\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\tclient, err := b.remoteClient(name)\n\tif err != nil {\n\t\treturn nil, diags.Append(err)\n\t}\n\tstateMgr := &remote.State{Client: client}\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote-state/oss/backend_state.go#L91-L127","documentation":"DeleteWorkspace() (backend_state.go:108-110) refuses to delete the workspace named backend.DefaultStateName (\"default\") or an empty name. This is an intentional guard so the always-present default state cannot be destroyed, which would orphan the bucket object namespace. It is a usage error, not a system fault.","triggerScenarios":"Invoking terraform workspace delete default (or passing \"\" to DeleteWorkspace). The guard short-circuits before any OSS/OTS call.","commonSituations":"Scripted workspace cleanup that loops over all workspace names including \"default\"; CI trying to tear down every workspace; a tool wrapper that passes an empty name.","solutions":["Exclude \"default\" from any scripted deletion loop.","If you truly want the default state gone, delete the underlying OSS object and OTS digest row manually after removing all other workspaces.","Pass a real non-default workspace name to terraform workspace delete."],"exampleFix":"# before\nfor ws in $(terraform workspace list); do terraform workspace delete $ws; done\n\n# after\nfor ws in $(terraform workspace list | grep -v '^* default$' | grep -v '^default$'); do\n  terraform workspace delete \"$ws\"\ndone","handlingStrategy":"validation","validationCode":"// Exclude 'default' from deletion lists in any wrapper script.\nfunc deletable(name string) bool {\n    return name != \"\" && name != \"default\"\n}","typeGuard":null,"tryCatchPattern":"// This is an intentional guard; the 'fix' is to not call it. Treat as no-op.\nif err != nil && strings.Contains(err.Error(), \"can't delete default state\") {\n    return nil // expected; skip\n}","preventionTips":["Filter 'default' out of scripted workspace teardown loops.","Document that the default workspace is immutable-by-design in team runbooks.","Use named workspaces for ephemeral environments instead of repurposing default."],"tags":["terraform","workspaces","remote-state","validation","go"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}