hashicorp/terraform · warning
can't delete default state
Error message
can't delete default state
What it means
The in-memory backend refuses to delete its default workspace. DeleteWorkspace() checks the requested name against backend.DefaultStateName ("default") and the empty string, and returns this error to protect the always-present default state from removal.
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 c9def3e214)
Solutions
- Skip the workspace named 'default' (and the empty string) in any cleanup loop that deletes workspaces.
- Delete or recreate non-default workspaces only; the default workspace is managed by the backend and cannot be removed.
- To reset all in-memory state in tests, call inmem.Reset() directly rather than deleting individual workspaces.
Example fix
# before - deletes every workspace including default for ws in $(terraform workspace list); do terraform workspace delete "$ws"; done # after - skip the protected default workspace for ws in $(terraform workspace list); do [ "$ws" = "default" ] && continue terraform workspace delete "$ws" done
Defensive patterns
Strategy: validation
Validate before calling
// Guard any workspace cleanup loop against the protected default name
func deleteAllNonDefault(b backend.Backend, names []string) error {
for _, n := range names {
if n == "default" || n == "" {
continue
}
if diags := b.DeleteWorkspace(n, false); diags.HasErrors() {
return diags.Err()
}
}
return nil
} Prevention
- Always skip "default" (and empty) names in workspace deletion loops.
- Call inmem.Reset() in test teardown instead of deleting workspaces one by one.
- Treat the default workspace as undeletable by design.
When it happens
Trigger: Invoking `terraform workspace delete default` (or deleting an empty-named workspace) while using the `inmem` backend, which calls DeleteWorkspace at inmem/backend.go:114-124.
Common situations: CI/test scripts that programmatically clean up all workspaces without skipping 'default'; a test helper that iterates Workspaces() and deletes each one including the default.
Related errors
- failed to lock inmem state: %s
- can't delete default state
- HTTP error: %d
- Failed to configure: %s
- secret_suffix must not end with '-<number>', got %q
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/687ecae5cc54cb7d.
Report an issue: GitHub.