hashicorp/terraform · error

cannot delete default state

Error message

cannot delete default state

What it means

Returned by Local.DeleteWorkspace (internal/backend/local/backend.go:248) when name == backend.DefaultStateName ("default"). The default workspace is mandatory for every backend (it is the initial state) and cannot be removed; deleting it would leave the backend in an unusable state.

Source

Thrown at internal/backend/local/backend.go:248

}

// DeleteWorkspace removes a workspace.
//
// The "default" workspace cannot be removed.
func (b *Local) DeleteWorkspace(name string, force bool) tfdiags.Diagnostics {
	var diags tfdiags.Diagnostics

	// If we have a backend handling state, defer to that.
	if b.Backend != nil {
		return b.Backend.DeleteWorkspace(name, force)
	}

	if name == "" {
		return diags.Append(errors.New("empty state name"))
	}

	if name == backend.DefaultStateName {
		return diags.Append(errors.New("cannot delete default state"))
	}

	delete(b.states, name)
	err := os.RemoveAll(filepath.Join(b.stateWorkspaceDir(), name))
	if err != nil {
		return diags.Append(fmt.Errorf("error deleting workspace %s: %w", name, err))
	}

	return diags
}

func (b *Local) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

	// If we have a backend handling state, delegate to that.
	if b.Backend != nil {
		return b.Backend.StateMgr(name)
	}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Skip the 'default' workspace in any delete loop (check name == backend.DefaultStateName).
  2. Do not run 'terraform workspace delete default'; it is intentionally non-deletable.
  3. If you want to reset state, clear the state contents (terraform state rm / destroy) instead of deleting the default workspace.

Example fix

// before
for _, ws := range workspaces {
    b.DeleteWorkspace(ws, false) // deletes 'default' too -> error
}

// after
for _, ws := range workspaces {
    if ws == backend.DefaultStateName {
        continue
    }
    b.DeleteWorkspace(ws, false)
}
Defensive patterns

Strategy: validation

Validate before calling

if name == backend.DefaultStateName {
    return nil // default workspace is protected; skip
}
return b.DeleteWorkspace(name, force)

Prevention

When it happens

Trigger: Calling (*Local).DeleteWorkspace("default", force) regardless of the force flag; running 'terraform workspace delete default'.

Common situations: Cleanup scripts that iterate all workspaces and try to delete each including 'default'; users misunderstanding that the default workspace is protected; migration scripts that attempt to wipe all state.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/0b118ad563d60459. Report an issue: GitHub.