{"record":{"id":"ade6663d1971f9fc","repo":"hashicorp/terraform","slug":"q-is-not-a-valid-state-name","errorCode":null,"errorMessage":"%q is not a valid state name","messagePattern":"%q is not a valid state name","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/gcs/backend_state.go","lineNumber":83,"sourceCode":"// 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,\n\t\tbucketName:    b.bucketName,\n\t\tstateFilePath: b.stateFile(name),\n\t\tlockFilePath:  b.lockFile(name),\n\t\tencryptionKey: b.encryptionKey,\n\t\tkmsKeyName:    b.kmsKeyName,\n\t}, nil\n}\n\n// StateMgr reads and returns the named state from GCS. If the named state does\n// not yet exist, a new state file is created.\nfunc (b *Backend) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\tc, err := b.client(name)","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote-state/gcs/backend_state.go#L65-L101","documentation":"Returned by the backend's client factory when the requested workspace name is the empty string. The factory refuses to construct a remoteClient for an empty name because it would resolve to an ambiguous state file path.","triggerScenarios":"client(name) is called with name == \"\" — typically because a workspace lookup returned empty or an upstream caller passed an unset variable.","commonSituations":"A script computes a workspace name from an env var that is unset; programmatic use of the backend passes an empty string; workspace name came from a malformed config map key.","solutions":["Ensure the workspace name passed into the operation is non-empty before calling backend methods.","Default to backend.DefaultStateName when no workspace is selected.","Check the caller (e.g., terraform workspace selection) and fix the empty-name source."],"exampleFix":"// before\nname := os.Getenv(\"TF_WORKSPACE\")  // empty when unset\nc, err := b.client(name)\n// after\nname := os.Getenv(\"TF_WORKSPACE\")\nif name == \"\" { name = backend.DefaultStateName }\nc, err := b.client(name)","handlingStrategy":"validation","validationCode":"name := os.Getenv(\"TF_WORKSPACE\")\nif name == \"\" {\n    name = backend.DefaultStateName\n}\nif name == \"\" {\n    return fmt.Errorf(\"workspace name is empty\")\n}","typeGuard":"func isValidWorkspaceName(name string) bool { return name != \"\" }","tryCatchPattern":null,"preventionTips":["Default workspace selection to backend.DefaultStateName when unset.","Validate computed workspace names against emptiness before invoking backend ops."],"tags":["gcs","backend","workspaces","validation","state-management"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}