{"record":{"id":"0b118ad563d60459","repo":"hashicorp/terraform","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":248,"sourceCode":"}\n\n// DeleteWorkspace removes a workspace.\n//\n// The \"default\" workspace cannot be removed.\nfunc (b *Local) DeleteWorkspace(name string, force bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\t// If we have a backend handling state, defer to that.\n\tif b.Backend != nil {\n\t\treturn b.Backend.DeleteWorkspace(name, force)\n\t}\n\n\tif name == \"\" {\n\t\treturn diags.Append(errors.New(\"empty state name\"))\n\t}\n\n\tif name == backend.DefaultStateName {\n\t\treturn diags.Append(errors.New(\"cannot delete default state\"))\n\t}\n\n\tdelete(b.states, name)\n\terr := os.RemoveAll(filepath.Join(b.stateWorkspaceDir(), name))\n\tif err != nil {\n\t\treturn diags.Append(fmt.Errorf(\"error deleting workspace %s: %w\", name, err))\n\t}\n\n\treturn diags\n}\n\nfunc (b *Local) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\t// If we have a backend handling state, delegate to that.\n\tif b.Backend != nil {\n\t\treturn b.Backend.StateMgr(name)\n\t}","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/local/backend.go#L230-L266","documentation":"Returned by Local.DeleteWorkspace (internal/backend/local/backend.go:248) when name == backend.DefaultStateName (\"default\"). The default workspace is mandatory for every backend (it is the initial state) and cannot be removed; deleting it would leave the backend in an unusable state.","triggerScenarios":"Calling (*Local).DeleteWorkspace(\"default\", force) regardless of the force flag; running 'terraform workspace delete default'.","commonSituations":"Cleanup scripts that iterate all workspaces and try to delete each including 'default'; users misunderstanding that the default workspace is protected; migration scripts that attempt to wipe all state.","solutions":["Skip the 'default' workspace in any delete loop (check name == backend.DefaultStateName).","Do not run 'terraform workspace delete default'; it is intentionally non-deletable.","If you want to reset state, clear the state contents (terraform state rm / destroy) instead of deleting the default workspace."],"exampleFix":"// before\nfor _, ws := range workspaces {\n    b.DeleteWorkspace(ws, false) // deletes 'default' too -> error\n}\n\n// after\nfor _, ws := range workspaces {\n    if ws == backend.DefaultStateName {\n        continue\n    }\n    b.DeleteWorkspace(ws, false)\n}","handlingStrategy":"validation","validationCode":"if name == backend.DefaultStateName {\n    return nil // default workspace is protected; skip\n}\nreturn b.DeleteWorkspace(name, force)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Skip backend.DefaultStateName in any workspace-deletion loop.","Never expose a UI affordance to delete the default workspace.","Treat the default workspace as non-deletable in all tooling."],"tags":["backend","local","workspace","default-state","protected"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}