{"record":{"id":"6df18ddbcb5b64e8","repo":"Budibase/budibase","slug":"custom-rest-template-cannot-be-deleted-while-it-is","errorCode":null,"errorMessage":"Custom REST template cannot be deleted while it is in use","messagePattern":"Custom REST template cannot be deleted while it is in use","errorType":"http","errorClass":"HTTPError","httpStatus":409,"severity":"warning","filePath":"packages/server/src/api/controllers/restTemplate.ts","lineNumber":154,"sourceCode":"\nexport const destroy = async (\n  ctx: UserCtx<\n    void,\n    DeleteCustomRestTemplateResponse,\n    { restTemplateId: string }\n  >\n) => {\n  const { restTemplateId } = ctx.params\n  if (!isCustomRestTemplateId(restTemplateId)) {\n    throw new HTTPError(\"Invalid custom REST template ID\", 400)\n  }\n\n  const removalResult = await sdk.restTemplates.removeIfUnused(restTemplateId)\n  if (removalResult === \"missing\") {\n    throw new HTTPError(\"Custom REST template not found\", 404)\n  }\n  if (removalResult === \"in_use\") {\n    throw new HTTPError(\n      \"Custom REST template cannot be deleted while it is in use\",\n      409\n    )\n  }\n  ctx.body = {\n    message: `Custom REST template ${restTemplateId} deleted`,\n  }\n}\n","sourceCodeStart":136,"sourceCodeEnd":163,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/api/controllers/restTemplate.ts#L136-L163","documentation":"This 409 Conflict HTTPError is returned when removeIfUnused determines the custom template is still referenced by one or more datasources. Deletion is blocked to avoid breaking datasources whose queries derive from the template.","triggerScenarios":"DELETE on a custom REST template that is currently selected as the source of an existing REST datasource in the app.","commonSituations":"Teams sharing an app where someone tries to clean up a template another user wired into a datasource; deleting a template right after creating a datasource from it.","solutions":["Find and delete (or repoint) all datasources using this template, then retry the DELETE","Use a different template for new datasources and migrate existing ones first","Accept the 409 and surface a message asking the user to remove dependent datasources"],"exampleFix":"// before\nDELETE /api/restTemplates/:id  // 409 in_use\n// after\nawait deleteDependentDatasources(templateId)\nawait api.delete(`/api/restTemplates/${templateId}`) // now succeeds","handlingStrategy":"try-catch","validationCode":"const datasources = await fetchDatasources()\nconst inUse = datasources.some(d => d.source === dsSource && usesTemplate(d, id))\nif (inUse) throw new Error(\"Template is used by existing datasources\")","typeGuard":"null","tryCatchPattern":"try {\n  await deleteTemplate(id)\n} catch (e) {\n  if (e?.status === 409) {\n    // prompt user to remove dependent datasources first\n  } else throw e\n}","preventionTips":["List datasources referencing the template before deleting","Repoint or delete dependent datasources first","Communicate the 409 in-use rule to users in the delete confirmation dialog"],"tags":["http-409","conflict","rest-template","referential-integrity"],"backgroundTag":"resource-in-use","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}