{"record":{"id":"5fd1a0d9e701f2ef","repo":"opentofu/opentofu","slug":"cannot-delete-default-state","errorCode":null,"errorMessage":"cannot delete default state","messagePattern":"cannot delete default state","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/local/backend.go","lineNumber":242,"sourceCode":"\n\treturn envs, nil\n}\n\n// DeleteWorkspace removes a workspace.\n//\n// The \"default\" workspace cannot be removed.\nfunc (b *Local) DeleteWorkspace(ctx context.Context, name string, force bool) error {\n\t// If we have a backend handling state, defer to that.\n\tif b.Backend != nil {\n\t\treturn b.Backend.DeleteWorkspace(ctx, name, force)\n\t}\n\n\tif name == \"\" {\n\t\treturn errors.New(\"empty state name\")\n\t}\n\n\tif name == backend.DefaultStateName {\n\t\treturn errors.New(\"cannot delete default state\")\n\t}\n\n\tdelete(b.states, name)\n\treturn os.RemoveAll(filepath.Join(b.stateWorkspaceDir(), name))\n}\n\nfunc (b *Local) StateMgr(ctx context.Context, name string) (statemgr.Full, error) {\n\t// If we have a backend handling state, delegate to that.\n\tif b.Backend != nil {\n\t\treturn b.Backend.StateMgr(ctx, name)\n\t}\n\n\tif s, ok := b.states[name]; ok {\n\t\treturn s, nil\n\t}\n\n\tif err := b.createState(name); err != nil {\n\t\treturn nil, err","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/opentofu/opentofu/blob/3561785c48c1ce615e7c50261bd351f26053efa2/internal/backend/local/backend.go#L224-L260","documentation":"Returned by (*local.Local).DeleteWorkspace in internal/backend/local/backend.go when asked to delete the workspace named backend.DefaultStateName (\"default\"). The default workspace is structural: it always exists and holds terraform.tfstate, so the backend refuses to remove it by design (see the doc comment 'The \"default\" workspace cannot be removed').","triggerScenarios":"Executing `tofu workspace delete default` against a local/filesystem backend, or calling DeleteWorkspace(ctx, backend.DefaultStateName, force) programmatically; the force flag does not bypass the check.","commonSituations":"Cleanup scripts that try to delete every workspace returned by `tofu workspace list`; scripts assuming all workspaces are deletable; attempts to reset state by deleting the default workspace.","solutions":["Skip the default workspace in deletion loops (delete every name except \"default\")","To reset the default workspace's contents, run `tofu destroy` instead of deleting the workspace","To discard state entirely, remove/backup terraform.tfstate on disk manually rather than via workspace delete"],"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":"if name == backend.DefaultStateName {\n    // default workspace cannot be deleted; skip or refuse early\n    return fmt.Errorf(\"refusing to delete the default workspace\")\n}\nerr := b.DeleteWorkspace(ctx, name, force)","typeGuard":null,"tryCatchPattern":"if err := b.DeleteWorkspace(ctx, name, force); err != nil {\n    if strings.Contains(err.Error(), \"cannot delete default state\") {\n        // expected guard: exclude 'default' from deletion loops\n    }\n}","preventionTips":["Always filter 'default' out of workspace-deletion loops","Use `tofu destroy` to reset default-workspace state instead of deleting the workspace"],"tags":["backend","local","workspaces","guard"],"backgroundTag":null,"analyzedSha":"3561785c48c1ce615e7c50261bd351f26053efa2","analyzedAt":"2026-08-15T23:27:16.226Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}