{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote/backend_state.go#L149-L185","documentation":"Emitted by remoteClient.Delete (backend_state.go:163-171) when Workspaces.Delete fails and the error is not tfe.ErrResourceNotFound. Delete is invoked to remove the workspace; ErrResourceNotFound is tolerated (already gone) but any other API failure is reported with the workspace name and cause.","triggerScenarios":"Calling workspace deletion while the workspace has locked state, pending runs, resources, or the token lacks admin/delete permission; or the TFC API returns 5xx/network error.","commonSituations":"Attempting to delete a workspace that still manages resources or has an in-flight run; insufficient permissions on the service account; TFC transient outage; workspace is locked by another user.","solutions":["Confirm and resolve preconditions in TFC UI: delete/queue resources, cancel pending runs, unlock the workspace first.","Grant the token's team/user the 'Admin' or 'Delete' permission on the workspace.","Retry after clearing the preconditions; transient 5xx resolve on retry.","If the workspace is already gone but a non-404 error returned, re-fetch the workspace to confirm its state before retrying."],"exampleFix":"// before\nError: error deleting workspace dev: 422 workspace has locked state\n\n// after: unlock then delete\n$ terraform force-unlock <lock-id>  # or unlock in UI\n$ terraform workspace delete dev","handlingStrategy":"validation","validationCode":"// Preflight: confirm the workspace has no locked state or pending runs before deleting.\nws, err := r.client.Workspaces.Read(ctx, r.organization, r.workspace.Name)\nif err != nil { return err }\nif ws.Locked { return errors.New(\"workspace is locked; unlock before deleting\") }","typeGuard":null,"tryCatchPattern":"// Tolerate already-deleted; surface everything else.\nif err := r.client.Workspaces.Delete(ctx, r.organization, r.workspace.Name); err != nil && err != tfe.ErrResourceNotFound {\n    return fmt.Errorf(\"error deleting workspace %s: %v\", r.workspace.Name, err)\n}","preventionTips":["Ensure the workspace has no resources/pending runs and is unlocked before delete.","Grant the token admin/delete permission on the workspace.","Retry transient 5xx; only surface persistent errors.","Confirm the workspace still exists (Read) before retrying to avoid confusing failures."],"tags":["workspace","delete","permissions","terraform-cloud"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}