{"record":{"id":"666ac80c9d6065db","repo":"paperclipai/paperclip","slug":"errormessage-error-cleanup-also-failed-for-new","errorCode":null,"errorMessage":"${errorMessage(error)} Cleanup also failed for newly-created company ${company.id}: ${errorMessage(cleanupError)}","messagePattern":"(.+?) Cleanup also failed for newly-created company (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/test-drive.ts","lineNumber":388,"sourceCode":"      await input.api.post<Agent>(`/api/companies/${company.id}/agents`, {\n        name: resolved.agentName,\n        role: \"ceo\",\n        adapterType: resolved.adapterType,\n        adapterConfig,\n      }),\n      \"creating the CEO agent\",\n    );\n\n    if (input.linkedWorktree) {\n      await reconcileTestDriveWorktreeExecution(input.api, input.instanceId);\n    }\n    return { reused: false, company, agent };\n  } catch (error) {\n    if (company) {\n      try {\n        await input.api.delete(`/api/companies/${company.id}`);\n      } catch (cleanupError) {\n        throw new Error(\n          `${errorMessage(error)} Cleanup also failed for newly-created company ${company.id}: ${errorMessage(cleanupError)}`,\n          { cause: error },\n        );\n      }\n    }\n    throw error;\n  }\n}\n\nfunction dashboardUrl(server: StartedServer): string {\n  return server.apiUrl.replace(/\\/api\\/?$/, \"\");\n}\n\nexport async function testDriveCommand(\n  options: TestDriveOptions,\n  dependencies: TestDriveDependencies = {\n    run: runCommand,\n    createApi: (apiBase) => new PaperclipApiClient({ apiBase }),","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/paperclipai/paperclip/blob/01ad8584922b5d85292b1723cae71fa0d9b07a19/cli/src/commands/test-drive.ts#L370-L406","documentation":"During bootstrapTestDrive, if creating the agent after company creation fails, the code compensates by deleting the newly created company. If that cleanup DELETE also fails, it throws a combined error carrying both the original failure and the cleanup failure, with the original as `cause`. This indicates a partially-created company was left behind on the server.","triggerScenarios":"Agent creation fails (e.g. invalid adapter config, API error) and the subsequent DELETE /api/companies/:id fails (e.g. transient network error, server rejects deletion due to existing references, auth expiry).","commonSituations":"Network blip mid-bootstrap leaving an orphaned test-drive company; company deletion blocked by foreign references in a newer server version; expired API credentials between the create and delete calls.","solutions":["Manually delete the orphaned company: DELETE /api/companies/<company.id> (or via the board UI)","Inspect the `cause` of the error to fix the original bootstrap failure before retrying","Retry the test-drive; if the same company name collides, remove the stale company first"],"exampleFix":"// after failure, clean up manually:\ncurl -X DELETE http://localhost:3100/api/companies/<company-id>","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await bootstrapTestDrive(opts);\n} catch (e) {\n  const root = e.cause ?? e;\n  console.error('bootstrap failed:', root.message);\n  console.error('check for orphaned companies and DELETE /api/companies/<id>');\n}","preventionTips":["Fix the root cause (in error.cause) before retrying to avoid duplicate orphan companies","Periodically list companies and delete stale test-drive entries","Ensure server credentials remain valid for the whole bootstrap so cleanup can succeed"],"tags":["cli","cleanup","api"],"backgroundTag":"api-request-failed","analyzedSha":"01ad8584922b5d85292b1723cae71fa0d9b07a19","analyzedAt":"2026-09-10T03:14:50.855Z","contentChangedAt":"2026-09-10T03:14:50.855Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}