{"record":{"id":"c0e46ef4c8268fcf","repo":"hashicorp/terraform","slug":"cowardly-refusing-to-delete-the-q-state","errorCode":null,"errorMessage":"cowardly refusing to delete the %q state","messagePattern":"cowardly refusing to delete the %q state","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/gcs/backend_state.go","lineNumber":69,"sourceCode":"\t\tif !strings.HasSuffix(name, stateFileSuffix) {\n\t\t\tcontinue\n\t\t}\n\t\tst := strings.TrimSuffix(name, stateFileSuffix)\n\n\t\tif st != backend.DefaultStateName {\n\t\t\tstates = append(states, st)\n\t\t}\n\t}\n\n\tsort.Strings(states[1:])\n\treturn states, diags\n}\n\n// DeleteWorkspace deletes the named workspaces. The \"default\" state cannot be deleted.\nfunc (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\tif name == backend.DefaultStateName {\n\t\treturn diags.Append(fmt.Errorf(\"cowardly refusing to delete the %q state\", name))\n\t}\n\n\tc, err := b.client(name)\n\tif err != nil {\n\t\treturn diags.Append(err)\n\t}\n\n\treturn diags.Append(c.Delete())\n}\n\n// client returns a remoteClient for the named state.\nfunc (b *Backend) client(name string) (*remoteClient, error) {\n\tif name == \"\" {\n\t\treturn nil, fmt.Errorf(\"%q is not a valid state name\", name)\n\t}\n\n\treturn &remoteClient{\n\t\tstorageClient: b.storageClient,","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote-state/gcs/backend_state.go#L51-L87","documentation":"Returned by DeleteWorkspace when the requested workspace name equals backend.DefaultStateName (\"default\"). The default state is the primary state and is protected from deletion regardless of the force flag.","triggerScenarios":"DeleteWorkspace is invoked with name == backend.DefaultStateName (\"default\"). The force argument is ignored — the guard is unconditional.","commonSituations":"An automation script iterates all workspaces and tries to delete each, including default; a user runs `terraform workspace delete default`; tooling that wraps `terraform workspace list` and deletes every entry.","solutions":["Skip the default workspace in any deletion loop: filter out name == \"default\".","Do not attempt `terraform workspace delete default`; the default workspace is non-deletable.","If you truly need to clear it, manually delete state objects in GCS rather than using DeleteWorkspace."],"exampleFix":"// before\nfor ws in $(terraform workspace list); do terraform workspace delete \"$ws\"; done\n// after\nfor ws in $(terraform workspace list); do [ \"$ws\" = \"default\" ] && continue; terraform workspace delete \"$ws\"; done","handlingStrategy":"validation","validationCode":"// Never call DeleteWorkspace for the default state.\nconst defaultName = \"default\"\nif name == defaultName {\n    return nil // or log and skip\n}\nreturn backend.DeleteWorkspace(name, force)","typeGuard":"func isDefaultWorkspace(name string) bool { return name == backend.DefaultStateName }","tryCatchPattern":null,"preventionTips":["Always filter 'default' out of workspace-deletion loops.","Treat the default workspace as non-deletable in automation scripts."],"tags":["gcs","backend","workspaces","state-management","guard"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}