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
- Pre-select a valid workspace with `terraform workspace select <name>` (add -or-create if needed) before the non-interactive run.
- Set TF_WORKSPACE to a workspace that exists in the backend.
- 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
- Run `terraform workspace select <name>` before non-interactive plan/apply.
- Keep TF_WORKSPACE in sync with workspaces that actually exist in the backend.
- Use `terraform workspace list` to confirm available workspaces in CI.
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
- Expected a single argument: NAME.
- The workspace name %q is not allowed. The name must contain
- Failed to select workspace: %s
- Failed to select workspace: input not a valid number
- default workspace not supported You can create a new workspa
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/4419a8a0252138e1.
Report an issue: GitHub.