opentofu/opentofu · error
cannot delete default state
Error message
cannot delete default state
What it means
Returned by (*local.Local).DeleteWorkspace in internal/backend/local/backend.go when asked to delete the workspace named backend.DefaultStateName ("default"). The default workspace is structural: it always exists and holds terraform.tfstate, so the backend refuses to remove it by design (see the doc comment 'The "default" workspace cannot be removed').
Source
Thrown at internal/backend/local/backend.go:242
return envs, nil
}
// DeleteWorkspace removes a workspace.
//
// The "default" workspace cannot be removed.
func (b *Local) DeleteWorkspace(ctx context.Context, name string, force bool) error {
// If we have a backend handling state, defer to that.
if b.Backend != nil {
return b.Backend.DeleteWorkspace(ctx, name, force)
}
if name == "" {
return errors.New("empty state name")
}
if name == backend.DefaultStateName {
return errors.New("cannot delete default state")
}
delete(b.states, name)
return os.RemoveAll(filepath.Join(b.stateWorkspaceDir(), name))
}
func (b *Local) StateMgr(ctx context.Context, name string) (statemgr.Full, error) {
// If we have a backend handling state, delegate to that.
if b.Backend != nil {
return b.Backend.StateMgr(ctx, name)
}
if s, ok := b.states[name]; ok {
return s, nil
}
if err := b.createState(name); err != nil {
return nil, errView on GitHub (pinned to 3561785c48)
Solutions
- Skip the default workspace in deletion loops (delete every name except "default")
- To reset the default workspace's contents, run `tofu destroy` instead of deleting the workspace
- To discard state entirely, remove/backup terraform.tfstate on disk manually rather than via workspace delete
Example fix
# before for ws in $(tofu workspace list | tr -d '* '); do tofu workspace delete "$ws"; done # after for ws in $(tofu workspace list | tr -d '* '); do [ "$ws" = "default" ] && continue tofu workspace delete "$ws" done
Defensive patterns
Strategy: validation
Validate before calling
if name == backend.DefaultStateName {
// default workspace cannot be deleted; skip or refuse early
return fmt.Errorf("refusing to delete the default workspace")
}
err := b.DeleteWorkspace(ctx, name, force) Try / catch
if err := b.DeleteWorkspace(ctx, name, force); err != nil {
if strings.Contains(err.Error(), "cannot delete default state") {
// expected guard: exclude 'default' from deletion loops
}
} Prevention
- Always filter 'default' out of workspace-deletion loops
- Use `tofu destroy` to reset default-workspace state instead of deleting the workspace
When it happens
Trigger: Executing `tofu workspace delete default` against a local/filesystem backend, or calling DeleteWorkspace(ctx, backend.DefaultStateName, force) programmatically; the force flag does not bypass the check.
Common situations: Cleanup scripts that try to delete every workspace returned by `tofu workspace list`; scripts assuming all workspaces are deletable; attempts to reset state by deleting the default workspace.
Related errors
- empty state name
- missing state name
- unsupported operation type: %s This is a bug in OpenTofu an
- failed to append certs
- the secret name %v is invalid, %s This is a requirement for
AI-assisted analysis of opentofu/opentofu@3561785c48 (2026-08-15).
Data as JSON: /api/errors/5fd1a0d9e701f2ef.
Report an issue: GitHub.