hashicorp/terraform · error
can't delete default state
Error message
can't delete default state
What it means
DeleteWorkspace (backend_state.go:62) refuses to delete the default workspace (or an empty-name workspace) because the default state always exists and cannot be removed; deleting it would orphan the backend's baseline state.
Source
Thrown at internal/backend/remote-state/consul/backend_state.go:62
envs[key] = struct{}{}
}
}
result := make([]string, 1, len(envs)+1)
result[0] = backend.DefaultStateName
for k, _ := range envs {
result = append(result, k)
}
return result, nil
}
func (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
if name == backend.DefaultStateName || name == "" {
return diags.Append(fmt.Errorf("can't delete default state"))
}
// Determine the path of the data
path := b.statePath(name)
// Delete it. We just delete it without any locking since
// the DeleteState API is documented as such.
_, err := b.client.KV().Delete(path, nil)
return diags.Append(err)
}
func (b *Backend) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
// Determine the path of the data
path := b.statePath(name)
// Determine whether to gzip or not
gzip := b.gzipView on GitHub (pinned to c9def3e214)
Solutions
- Do not attempt to delete the default workspace; skip it in automation.
- If you want to clear state, reset the default workspace's contents instead of deleting it.
- Guard deletion scripts to skip the default workspace name.
Example fix
# before for ws in $(terraform workspace list | sed 's/[* ]//g'); do terraform workspace delete "$ws"; done # after for ws in $(terraform workspace list | sed 's/[* ]//g'); do [ "$ws" = "default" ] && continue terraform workspace delete "$ws" done
Defensive patterns
Strategy: validation
Validate before calling
# never delete the default workspace ws="default" [ "$ws" != "default" ] && [ -n "$ws" ] \ && terraform workspace delete "$ws" \ || echo "skip: cannot delete default workspace"
Type guard
// guard deletion against the default/empty workspace name
func isDefaultWorkspace(name string) bool {
return name == "" || name == "default"
} Prevention
- Special-case 'default' in workspace cleanup scripts.
- Reset default state contents rather than deleting the workspace.
- Review automation that enumerates and deletes all workspaces.
When it happens
Trigger: Calling workspace deletion on the Consul backend for the default workspace (e.g. `terraform workspace delete default`) or for an empty workspace name.
Common situations: Automation that loops over `terraform workspace list` and deletes each entry; an accidental `terraform workspace delete default`; a script that does not special-case default.
Related errors
- consul lock was lost
- No state file was found! State management commands require
- error loading state: %w
- failed to lock state in Consul: %s
- Error unlocking Consul state. Lock ID: %s Error: %s You ma
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/b18a76c9b34ba80b.
Report an issue: GitHub.