hashicorp/terraform · error
Expected a workspace name as an argument, instead got an…
Error message
Expected a workspace name as an argument, instead got an empty string: %q
What it means
Thrown by terraform workspace delete when the NAME positional argument is provided but is an empty string. Terraform explicitly checks args[0]=="" separately from the missing-argument check to give a clearer error than a generic 'expected one argument' message.
Solutions
- Check that the workspace name variable is set and non-empty before invoking the command
- Use shell parameter expansion with a guard: terraform workspace delete "${WS_NAME:?workspace name required}"
- Verify the argument is passed correctly by echoing it before the command: echo "[$WS_NAME]"
Example fix
# before WS_NAME="" terraform workspace delete "$WS_NAME" # after WS_NAME="dev" terraform workspace delete "$WS_NAME"
Defensive patterns
Strategy: validation
Validate before calling
// Validate workspace name argument before calling terraform workspace delete
func validateWorkspaceNameArg(name string) error {
if name == "" {
return errors.New("workspace name must not be empty")
}
return nil
} Prevention
- Always check shell variables are set and non-empty before passing as workspace name
- Use ${VAR:?error message} shell expansion to fail fast on empty variables
- Validate workspace names in automation scripts before invoking terraform
- Echo bracketed variables in debug output to catch empty strings: echo "[$WS_NAME]"
When it happens
Trigger: Running terraform workspace delete "" or passing an empty-string argument. The name=="" check triggers after confirming at least one argument was provided but before checking for extra arguments.
Common situations: Shell variable expansion producing an empty string (e.g., terraform workspace delete "$WS_NAME" where WS_NAME is unset or empty); quoting errors in scripts; automated tooling or wrappers passing an empty workspace name.
Related errors
- can't delete default state
- Couldn't create initial workspace: no name provided
- Error creating workspace
- Failed to select workspace: input not a valid number
- Invalid workspace name set using
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/2f9d6ce9bc32096c.
Report an issue: GitHub.
Appendix: source
Thrown at internal/command/arguments/workspace_delete.go:62
err.Error(),
))
}
// `workspace delete` takes only one positional argument: workspace name.
args = cmdFlags.Args()
var name string
if len(args) == 0 {
diags = diags.Append(errors.New("Expected a single argument: NAME.")) // Recreating pre-existing error from command package
} else {
// Obtain and validate name argument
//
// We purposefully don't use ValidWorkspaceName here; if a user
// creates a workspace with an invalid name they should be able to
// delete it easily.
name = args[0]
if name == "" {
diags = diags.Append(fmt.Errorf("Expected a workspace name as an argument, instead got an empty string: %q\n", args[0]))
}
// Checking for extra arguments here, not with a len != 1 check above, allows the name to be returned
args = args[1:]
if len(args) != 0 {
diags = diags.Append(errors.New("Expected a single argument: NAME."))
}
}
return &WorkspaceDelete{
Workspace: Workspace{ViewType: ViewHuman},
Name: name,
Lock: stateLock,
LockTimeout: stateLockTimeout,
Force: force,
}, diags
}
View on GitHub (pinned to d32a084675)