{"record":{"id":"8417b20ab057793e","repo":"hashicorp/terraform","slug":"can-t-delete-default-state-8417b2","errorCode":null,"errorMessage":"can't delete default state","messagePattern":"can't delete default state","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/oci/backend_state.go","lineNumber":195,"sourceCode":"\t\t\t\t\twss = append(wss, name)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif len(listObjectResponse.Objects) < maxKeys {\n\t\t\tbreak\n\t\t}\n\t\tstart = listObjectResponse.NextStartWith\n\n\t}\n\n\treturn uniqueStrings(wss), diags\n}\n\nfunc (b *Backend) DeleteWorkspace(name string, force bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tif name == backend.DefaultStateName || name == \"\" {\n\t\treturn diags.Append(fmt.Errorf(\"can't delete default state\"))\n\t}\n\tif b.client == nil {\n\t\terr := b.configureRemoteClient()\n\t\tif err != nil {\n\t\t\treturn diags.Append(err)\n\t\t}\n\t}\n\n\tb.client.path = b.path(name)\n\tb.client.lockFilePath = b.getLockFilePath(name)\n\treturn diags.Append(b.client.Delete())\n\n}\n","sourceCodeStart":177,"sourceCodeEnd":209,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote-state/oci/backend_state.go#L177-L209","documentation":"DeleteWorkspace rejects deleting the workspace named \"default\" or \"\" (backend.DefaultStateName). The default workspace holds the primary state and is intentionally protected from deletion through this API. The error is a hard usage error, not a runtime failure.","triggerScenarios":"Running `terraform workspace delete default`; calling backend.DeleteWorkspace(\"default\") or DeleteWorkspace(\"\") programmatically; automation that enumerates workspaces and tries to delete every entry including default.","commonSituations":"Cleanup scripts that loop over `terraform workspace list` and delete each; misunderstood workspace model where users assume default is removable; CI teardown logic that does not skip default.","solutions":["Do not delete the default workspace — skip it in any cleanup loop.","If you need to clear its state, run `terraform destroy` against the default workspace instead of deleting it.","To remove the state object entirely, destroy resources first, then delete the state object in the bucket manually (this loses Terraform's tracking)."],"exampleFix":"// before: deletes every workspace returned by Workspaces()\nfor _, w := range workspaces {\n    backend.DeleteWorkspace(w, false)\n}\n// after: skip the protected default workspace\nfor _, w := range workspaces {\n    if w == \"\" || w == backend.DefaultStateName {\n        continue\n    }\n    backend.DeleteWorkspace(w, false)\n}","handlingStrategy":"validation","validationCode":"// Guard before calling DeleteWorkspace\nfunc safeDelete(b *Backend, name string, force bool) tfdiags.Diagnostics {\n    if name == \"\" || name == backend.DefaultStateName {\n        return nil // nothing to do; default is protected\n    }\n    return b.DeleteWorkspace(name, force)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Filter 'default' out of any workspace enumeration before deleting.","Document that the default workspace is intentionally non-deletable.","In CI teardown, keep an allowlist of workspaces to delete rather than deleting all.","Treat an empty workspace name as default-equivalent in your tooling."],"tags":["oci","workspaces","validation","terraform"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}