hashicorp/terraform · error

Currently selected workspace %q does not exist

Error message

Currently selected workspace %q does not exist

What it means

Returned by selectWorkspace() when input is disabled (m.input==false), there are multiple workspaces in the backend, and the currently selected workspace name is not among them. Because Terraform cannot prompt the user, it fails fast rather than silently picking the wrong state. The %q is the missing workspace name.

Source

Thrown at internal/command/meta_backend.go:299

	// Check if any of the existing workspaces matches the selected
	// workspace and create a numbered list of existing workspaces.
	var list strings.Builder
	for i, w := range workspaces {
		if w == workspace {
			log.Printf("[TRACE] Meta.selectWorkspace: the currently selected workspace is present in the configured backend (%s)", workspace)
			return nil
		}
		fmt.Fprintf(&list, "%d. %s\n", i+1, w)
	}

	// If the backend only has a single workspace, select that as the current workspace
	if len(workspaces) == 1 {
		log.Printf("[TRACE] Meta.selectWorkspace: automatically selecting the single workspace provided by the backend (%s)", workspaces[0])
		return m.SetWorkspace(workspaces[0])
	}

	if !m.input {
		return fmt.Errorf("Currently selected workspace %q does not exist", workspace)
	}

	// Otherwise, ask the user to select a workspace from the list of existing workspaces.
	v, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{
		Id: "select-workspace",
		Query: fmt.Sprintf(
			"\n[reset][bold][yellow]The currently selected workspace (%s) does not exist.[reset]",
			workspace),
		Description: fmt.Sprintf(
			strings.TrimSpace(inputBackendSelectWorkspace), list.String()),
	})
	if err != nil {
		return fmt.Errorf("Failed to select workspace: %s", err)
	}

	idx, err := strconv.Atoi(v)
	if err != nil || (idx < 1 || idx > len(workspaces)) {
		return fmt.Errorf("Failed to select workspace: input not a valid number")

View on GitHub (pinned to c9def3e214)

Solutions

  1. Pre-select a valid workspace with `terraform workspace select <name>` (add -or-create if needed) before the non-interactive run.
  2. Set TF_WORKSPACE to a workspace that exists in the backend.
  3. Create the missing workspace: `terraform workspace new <name>`.

Example fix

# before: CI non-interactive, stale workspace
terraform plan -input=false
# Currently selected workspace "old" does not exist
# after
terraform workspace select prod
terraform plan -input=false
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate that the selected workspace exists before non-interactive run
name := getSelectedWorkspace()
ws, _ := backendWorkspaces()
if !contains(ws, name) {
    log.Fatalf("workspace %q not found; available: %v", name, ws)
}

Prevention

When it happens

Trigger: Backend returns >1 workspaces, none equal the locally selected workspace (from .terraform/environment or TF_WORKSPACE), and -input=false (or non-interactive) prevents the selection prompt.

Common situations: CI runs with -input=false where the workspace was deleted/renamed remotely; switching branches where TF_WORKSPACE points to a workspace that doesn't exist in this backend; a fresh clone where no workspace has been selected yet but TF_WORKSPACE names a non-existent one.

Related errors


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