{"record":{"id":"a7fda44dbb0be3cf","repo":"hashicorp/terraform","slug":"error-deleting-workspace-s-v","errorCode":null,"errorMessage":"error deleting workspace %s: %v","messagePattern":"error deleting workspace (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote/backend_state.go","lineNumber":167,"sourceCode":"\tif errors.Is(err, tfe.ErrStateVersionUploadNotSupported) {\n\t\t// Create the new state with content included in the request (Terraform Enterprise v202306-1 and below)\n\t\tlog.Println(\"[INFO] Detected that state version upload is not supported. Retrying using compatibility state upload.\")\n\t\treturn diags.Append(r.uploadStateFallback(ctx, stateFile, state, o))\n\t}\n\tif err != nil {\n\t\tr.stateUploadErr = true\n\t\treturn diags.Append(fmt.Errorf(\"error uploading state: %v\", err))\n\t}\n\n\treturn nil\n}\n\n// Delete the remote state.\nfunc (r *remoteClient) Delete() tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\terr := r.client.Workspaces.Delete(context.Background(), r.organization, r.workspace.Name)\n\tif err != nil && err != tfe.ErrResourceNotFound {\n\t\treturn diags.Append(fmt.Errorf(\"error deleting workspace %s: %v\", r.workspace.Name, err))\n\t}\n\n\treturn nil\n}\n\n// EnableForcePush to allow the remote client to overwrite state\n// by implementing remote.ClientForcePusher\nfunc (r *remoteClient) EnableForcePush() {\n\tr.forcePush = true\n}\n\n// Lock the remote state.\nfunc (r *remoteClient) Lock(info *statemgr.LockInfo) (string, error) {\n\tctx := context.Background()\n\n\tlockErr := &statemgr.LockError{Info: r.lockInfo}\n\n\t// Lock the workspace.","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote/backend_state.go#L149-L185","documentation":"remoteClient.Delete calls Workspaces.Delete — invoked when Terraform is asked to delete the workspace backing the remote state (e.g. `terraform workspace delete`). ErrResourceNotFound is tolerated (already gone); anything else is surfaced with the workspace name.","triggerScenarios":"Workspaces.Delete fails: the workspace is not empty (still has state versions / runs), it is currently locked, the token lacks 'Admin' / 'Delete Workspaces' permission, 5xx, or network error.","commonSituations":"Trying to delete a workspace that still has state, runs in progress, or a lock; team token without admin scope; deleting from the CLI while a TFC run is queued; TFE maintenance window.","solutions":["Delete the workspace's state and pending runs first (or use TFC UI which orchestrates this).","Unlock the workspace if it is locked before retrying delete.","Use a token with admin-level permission on the workspace.","If already deleted out-of-band, the next attempt tolerates ErrResourceNotFound."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Confirm workspace is empty/unlocked before delete\nws, _ := c.Workspaces.Read(ctx, org, name)\nif ws.Locked { return fmt.Errorf(\"workspace %s is locked\", name) }","typeGuard":null,"tryCatchPattern":"// Tolerate 'already deleted' but surface real failures\nif err := c.Workspaces.Delete(ctx, org, name); err != nil && err != tfe.ErrResourceNotFound {\n    return err\n}","preventionTips":["Delete state/runs and unlock the workspace before calling Workspaces.Delete.","Use a token with admin scope for workspace lifecycle ops.","Prefer the TFC UI's orchestrated delete over raw CLI delete."],"tags":["terraform","remote-backend","tfe","workspace","lifecycle","permissions"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}