hashicorp/terraform · error

can't delete default state

Error message

can't delete default state

What it means

Returned by Backend.DeleteWorkspace (s3/backend_state.go:142) when the requested workspace name equals backend.DefaultStateName ("default") or is empty. Identical guard to the Postgres backend: the default workspace is protected and cannot be removed.

Source

Thrown at internal/backend/remote-state/s3/backend_state.go:142

	// not our key, so don't include it in our listing
	if parts[1] != b.keyName {
		return ""
	}

	return parts[0]
}

func (b *Backend) DeleteWorkspace(name string, _ bool) tfdiags.Diagnostics {
	var diags tfdiags.Diagnostics

	log := logger()
	log = logWithOperation(log, operationBackendDeleteWorkspace)
	log = log.With(
		logKeyBackendWorkspace, name,
	)

	if name == backend.DefaultStateName || name == "" {
		return diags.Append(fmt.Errorf("can't delete default state"))
	}

	log.Info("Deleting workspace")

	client, err := b.remoteClient(name)
	if err != nil {
		return diags.Append(err)
	}

	return diags.Append(client.Delete())
}

// get a remote client configured for this state
func (b *Backend) remoteClient(name string) (*RemoteClient, error) {
	if name == "" {
		return nil, errors.New("missing state name")
	}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Skip "default" and empty names in deletion loops.
  2. To reset default's contents, destroy resources or rewrite state rather than deleting the workspace.
  3. Validate the name before calling DeleteWorkspace.

Example fix

// before
// for _, ws := range backend.Workspaces() { backend.DeleteWorkspace(ws, false) }

// after
// for _, ws := range backend.Workspaces() {
//   if ws == "default" || ws == "" { continue }
//   backend.DeleteWorkspace(ws, false)
// }
Defensive patterns

Strategy: validation

Validate before calling

// Guard the protected name before deleting
// if name == "" || name == backend.DefaultStateName { return errors.New("protected") }
// backend.DeleteWorkspace(name, false)

Prevention

When it happens

Trigger: Running `terraform workspace delete default` or `""`; programmatic DeleteWorkspace("default"); bulk cleanup scripts iterating all workspaces without skipping default.

Common situations: Automation that deletes every returned workspace; migration scripts passing an empty name; tooling assuming all listed workspaces are deletable.

Related errors


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