{"record":{"id":"e485a2c29180297c","repo":"hashicorp/terraform","slug":"failed-to-retrieve-workspace-s-v-e485a2","errorCode":null,"errorMessage":"failed to retrieve workspace %s: %v","messagePattern":"failed to retrieve workspace (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloud/backend.go","lineNumber":716,"sourceCode":"// DeleteWorkspace implements backend.Backend (which is embedded in backendrun.OperationsBackend).\nfunc (b *Cloud) DeleteWorkspace(name string, force bool) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tif name == backend.DefaultStateName {\n\t\treturn diags.Append(backend.ErrDefaultWorkspaceNotSupported)\n\t}\n\n\tif b.WorkspaceMapping.Strategy() == WorkspaceNameStrategy {\n\t\treturn diags.Append(backend.ErrWorkspacesNotSupported)\n\t}\n\n\tworkspace, err := b.client.Workspaces.Read(context.Background(), b.Organization, name)\n\tif err == tfe.ErrResourceNotFound {\n\t\treturn nil // If the workspace does not exist, succeed\n\t}\n\n\tif err != nil {\n\t\treturn diags.Append(fmt.Errorf(\"failed to retrieve workspace %s: %v\", name, err))\n\t}\n\n\t// Configure the remote workspace name.\n\tState := &State{tfeClient: b.client, organization: b.Organization, workspace: workspace, enableIntermediateSnapshots: false}\n\treturn diags.Append(State.Delete(force))\n}\n\n// StateMgr implements backend.Backend (which is embedded in backendrun.OperationsBackend).\nfunc (b *Cloud) StateMgr(name string) (statemgr.Full, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\tvar remoteTFVersion string\n\n\tif name == backend.DefaultStateName {\n\t\treturn nil, diags.Append(backend.ErrDefaultWorkspaceNotSupported)\n\t}\n\n\tif b.WorkspaceMapping.Strategy() == WorkspaceNameStrategy && name != b.WorkspaceMapping.Name {","sourceCodeStart":698,"sourceCodeEnd":734,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/cloud/backend.go#L698-L734","documentation":"During DeleteWorkspace, the backend reads the workspace from the TFE/HCP API. If the read fails with any error other than tfe.ErrResourceNotFound (which is treated as success — the workspace doesn't exist so deletion is a no-op), the error is wrapped and returned. This prevents attempting to delete a workspace whose state cannot be retrieved.","triggerScenarios":"Calling b.client.Workspaces.Read at backend.go:710 during a workspace delete operation returns a non-nil, non-404 error. Causes include API authentication failures, permission errors, rate limiting, server-side errors, or network connectivity issues.","commonSituations":"The API token lacks permission to read the target workspace. Transient API errors during `terraform workspace delete`. Network issues between Terraform and the HCP Terraform / TFE host. The workspace name contains special characters or is incorrectly specified.","solutions":["Verify the workspace name is correct and the token has read access to it.","Retry the operation if the error was transient (network blip, rate limit).","Check that the organization name is correct in the cloud configuration.","Use the HCP Terraform / TFE UI to verify the workspace exists and the token has access."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Before deleting, verify workspace is accessible\n_, err := b.client.Workspaces.Read(ctx, b.Organization, name)\nif err != nil && err != tfe.ErrResourceNotFound {\n    return fmt.Errorf(\"cannot access workspace %s: %w\", name, err)\n}","typeGuard":null,"tryCatchPattern":"// Retry workspace read before delete\nfor i := 0; i < 3; i++ {\n    workspace, err := b.client.Workspaces.Read(ctx, b.Organization, name)\n    if err == nil {\n        // proceed with delete\n        break\n    }\n    if err == tfe.ErrResourceNotFound {\n        return nil // already gone\n    }\n    if i == 2 {\n        return diags.Append(fmt.Errorf(\"failed to retrieve workspace %s: %v\", name, err))\n    }\n    time.Sleep(time.Duration(1<<i) * time.Second)\n}","preventionTips":["Verify the workspace name and organization are correct before calling DeleteWorkspace.","Ensure the API token has workspace read and delete permissions.","Retry transient API errors with exponential backoff.","Use the HCP Terraform / TFE UI to confirm workspace existence and token access."],"tags":["terraform","cloud-backend","workspace","delete","tfe-api","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}