hashicorp/terraform · error
can't delete default state
Error message
can't delete default state
What it means
Thrown by the in-memory backend's DeleteWorkspace when the workspace name equals backend.DefaultStateName ("default") or is empty (backend.go:118-121). The default workspace is protected and cannot be deleted; only non-default workspaces can be removed from the inmem store.
Solutions
- Switch to a non-default workspace first, then delete that workspace — never delete 'default'.
- In cleanup scripts, skip name == "default" before calling DeleteWorkspace.
- To reset default state, use 'terraform destroy' or overwrite state rather than deleting the workspace.
Example fix
// before
for _, ws := range workspaces {
b.DeleteWorkspace(ws, false)
}
// after
for _, ws := range workspaces {
if ws == "default" || ws == "" {
continue
}
b.DeleteWorkspace(ws, false)
} Defensive patterns
Strategy: validation
Validate before calling
// Never delete the default workspace — guard before calling:
// if name == backend.DefaultStateName || name == "" {
// return errors.New("refusing to delete default workspace")
// }
// b.DeleteWorkspace(name, force) Try / catch
// err := b.DeleteWorkspace(name, force)
// if err != nil && strings.Contains(err.Error(), "can't delete default state") {
// // expected for the default workspace; skip
// } Prevention
- Always skip name == "default" in workspace-cleanup loops.
- Use 'terraform workspace select default' before deleting others.
- Reset default state with terraform destroy instead of workspace deletion.
When it happens
Trigger: Calling 'terraform workspace delete default' or programmatically invoking DeleteWorkspace("default") / DeleteWorkspace("") against the inmem backend.
Common situations: A script that loops over all workspaces and deletes them hits the default workspace; attempting to clean up by deleting the default workspace instead of switching off it; tests that delete workspaces indiscriminately.
Related errors
- can't delete default state
- can't delete default state
- can't delete default state
- can't delete default state
- can't delete default state
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/687ecae5cc54cb7d.
Report an issue: GitHub.
Appendix: source
Thrown at internal/backend/remote-state/inmem/backend.go:119
states.Lock()
defer states.Unlock()
var workspaces []string
for s := range states.m {
workspaces = append(workspaces, s)
}
sort.Strings(workspaces)
return workspaces, nil
}
func (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {
states.Lock()
defer states.Unlock()
if name == backend.DefaultStateName || name == "" {
return tfdiags.Diagnostics{}.Append(fmt.Errorf("can't delete default state"))
}
delete(states.m, name)
return nil
}
func (b *Backend) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
states.Lock()
defer states.Unlock()
s := states.m[name]
if s == nil {
s = &remote.State{
Client: &RemoteClient{
Name: name,
},View on GitHub (pinned to d32a084675)