{"record":{"id":"f2df334360cd7ddb","repo":"opentofu/opentofu","slug":"can-t-delete-default-state-f2df33","errorCode":null,"errorMessage":"can't delete default state","messagePattern":"can't delete default state","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/s3/backend_state.go","lineNumber":118,"sourceCode":"\t}\n\n\tparts = strings.SplitN(parts[1], \"/\", 2)\n\n\tif len(parts) < 2 {\n\t\treturn \"\"\n\t}\n\n\t// not our key, so don't include it in our listing\n\tif parts[1] != b.keyName {\n\t\treturn \"\"\n\t}\n\n\treturn parts[0]\n}\n\nfunc (b *Backend) DeleteWorkspace(ctx context.Context, name string, _ bool) error {\n\tif name == backend.DefaultStateName || name == \"\" {\n\t\treturn fmt.Errorf(\"can't delete default state\")\n\t}\n\n\tclient, err := b.remoteClient(name)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn client.Delete(ctx)\n}\n\n// get a remote client configured for this state\nfunc (b *Backend) remoteClient(name string) (*RemoteClient, error) {\n\tif name == \"\" {\n\t\treturn nil, errors.New(\"missing state name\")\n\t}\n\n\tclient := &RemoteClient{\n\t\ts3Client:              b.s3Client,","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/opentofu/opentofu/blob/3561785c48c1ce615e7c50261bd351f26053efa2/internal/backend/remote-state/s3/backend_state.go#L100-L136","documentation":"Returned by the S3 backend's DeleteWorkspace when the requested workspace name is \"default\" or empty. As with the Postgres backend, the default workspace is mandatory — it is the state addressed when no workspace is selected — so the name check short-circuits before any S3 call. There is no API path to delete the default workspace through this backend.","triggerScenarios":"`tofu workspace delete default`; automation that enumerates `tofu workspace list` and deletes each entry including the default one; direct calls to Backend.DeleteWorkspace with an empty string.","commonSituations":"Cleanup scripts and Makefile targets looping over workspaces; users trying to 'reset' a stack by deleting default.","solutions":["Skip `default` in any scripted deletion loop.","To neutralize default's contents, destroy its resources (tofu destroy) instead of deleting the workspace.","Create/switch to another workspace before deleting the one you intended."],"exampleFix":"# before\nfor ws in $(tofu workspace list | tr -d '* '); do tofu workspace delete \"$ws\"; done\n\n# after\nfor ws in $(tofu workspace list | tr -d '* '); do\n  [ \"$ws\" = \"default\" ] && continue\n  tofu workspace delete \"$ws\"\ndone","handlingStrategy":"validation","validationCode":"for _, ws := range workspaces {\n  if ws == \"default\" || ws == \"\" { continue }\n  if err := b.DeleteWorkspace(ctx, ws, false); err != nil { return err }\n}","typeGuard":"func isDefaultWorkspaceGuard(err error) bool {\n  return err != nil && strings.Contains(err.Error(), \"can't delete default state\")\n}","tryCatchPattern":"if err := b.DeleteWorkspace(ctx, name, false); err != nil {\n  if strings.Contains(err.Error(), \"can't delete default state\") {\n    // caller bug: adjust automation to never target default\n  }\n  return err\n}","preventionTips":["Exclude `default` in every scripted cleanup loop over workspaces.","Use `tofu destroy` to empty default rather than trying to delete it."],"tags":["s3","workspace","validation","backend"],"backgroundTag":null,"analyzedSha":"3561785c48c1ce615e7c50261bd351f26053efa2","analyzedAt":"2026-08-15T23:27:16.226Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}