{"record":{"id":"c1e5fc0ec16f9c5b","repo":"langfuse/langfuse","slug":"cannot-delete-organization-with-existing-projects","errorCode":null,"errorMessage":"Cannot delete organization with existing projects","messagePattern":"Cannot delete organization with existing projects","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"web/src/ee/features/admin-api/server/organizations/organizationById.ts","lineNumber":184,"sourceCode":"  }\n\n  // count non-deleted projects\n  const countNonDeletedProjects = await prisma.project.count({\n    where: {\n      orgId: organizationId,\n      deletedAt: null,\n    },\n  });\n\n  // count all projects (including soft-deleted)\n  const countAllProjects = await prisma.project.count({\n    where: {\n      orgId: organizationId,\n    },\n  });\n\n  if (countNonDeletedProjects > 0) {\n    return res.status(400).json({\n      error: \"Cannot delete organization with existing projects\",\n      message:\n        \"Please delete or transfer all projects before deleting the organization.\",\n    });\n  }\n\n  if (countAllProjects > 0) {\n    return res.status(400).json({\n      error: \"Cannot delete organization with existing projects\",\n      message:\n        \"Deletion of your projects is still being processed, please try deleting the organization later\",\n    });\n  }\n\n  // Delete the organization\n  const deletedOrganization = await prisma.organization.delete({\n    where: { id: organizationId },\n  });","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/langfuse/langfuse/blob/59d92c7cf365150d10b753b5a0d1708902a2ed60/web/src/ee/features/admin-api/server/organizations/organizationById.ts#L166-L202","documentation":"Returned as HTTP 400 when deleting an organization that still has one or more non-deleted projects (prisma.project.count where deletedAt: null > 0). The API refuses deletion until all projects are removed or transferred.","triggerScenarios":"DELETE /api/admin/organizations/:id while any project in the org has deletedAt = null.","commonSituations":"Forgetting to delete projects first in teardown scripts; soft-deleted-but-still-active projects; new projects created after the pre-delete listing.","solutions":["Delete (or transfer) every project in the organization first via the admin projects API","Re-check with the list projects endpoint until zero active projects remain","Then retry the organization delete"],"exampleFix":"// before\nawait adminApi.del(`/organizations/${orgId}`);\n// after\nfor (const p of await listProjects(orgId)) await adminApi.del(`/organizations/${orgId}/projects/${p.id}`);\nawait adminApi.del(`/organizations/${orgId}`);","handlingStrategy":"validation","validationCode":"const projects = await listProjects(orgId).then(ps => ps.filter(p => !p.deletedAt));\nif (projects.length > 0) throw new Error(`delete ${projects.length} projects first`);","typeGuard":null,"tryCatchPattern":"if (res.status === 400 && /existing projects/.test(body)) { await deleteAllProjects(orgId); retry; }","preventionTips":["Always drain projects before org deletion","Re-check for newly created projects right before delete"],"tags":["http-400","referential-integrity","delete-constraints","admin-api"],"backgroundTag":"dependent-resources-exist","analyzedSha":"59d92c7cf365150d10b753b5a0d1708902a2ed60","analyzedAt":"2026-08-27T22:22:00.402Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}