{"record":{"id":"abe2592561083be2","repo":"Budibase/budibase","slug":"agent-file-storage-not-found","errorCode":null,"errorMessage":"Agent file storage not found","messagePattern":"Agent file storage not found","errorType":"http","errorClass":"HTTPError","httpStatus":404,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/rag/files.ts","lineNumber":384,"sourceCode":"}\n\nexport const deleteFileForAgent = async (\n  agentId: string,\n  fileId: string\n): Promise<void> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  const fileKnowledgeBaseId = file.knowledgeBaseId\n  if (!fileKnowledgeBaseId) {\n    throw new HTTPError(\"Invalid knowledge base file id\", 400)\n  }\n  const knowledgeBaseIds = await getKnowledgeBaseIdsForAgent(agentId)\n  if (!knowledgeBaseIds.includes(fileKnowledgeBaseId)) {\n    throw new HTTPError(\"File does not belong to this agent\", 404)\n  }\n\n  const knowledgeBase = await knowledgeBaseSdk.find(fileKnowledgeBaseId)\n  if (!knowledgeBase) {\n    throw new HTTPError(\"Agent file storage not found\", 404)\n  }\n  await knowledgeBaseSdk.removeKnowledgeBaseFile(knowledgeBase, file)\n}\n\nexport const getFileUrlForAgent = async (\n  agentId: string,\n  fileId: string\n): Promise<string> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  const fileKnowledgeBaseId = file.knowledgeBaseId\n  if (!fileKnowledgeBaseId) {\n    throw new HTTPError(\"Invalid knowledge base file id\", 400)\n  }\n\n  const knowledgeBaseIds = await getKnowledgeBaseIdsForAgent(agentId)\n  if (!knowledgeBaseIds.includes(fileKnowledgeBaseId)) {\n    throw new HTTPError(\"File does not belong to this agent\", 404)\n  }","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/rag/files.ts#L366-L402","documentation":"Thrown by deleteFileForAgent after ownership passes but knowledgeBaseSdk.find() cannot locate the knowledge base referenced by the file. The file is still blocked from deletion because its parent storage entity is gone or unreadable.","triggerScenarios":"Calling deleteFileForAgent when the file's knowledgeBaseId points to a knowledge base that was deleted or exists in a different workspace.","commonSituations":"A knowledge base deleted out-of-band while files remained, cross-workspace ID reuse, or eventual consistency after deletion.","solutions":["Check whether the knowledge base still exists in this workspace before attempting deletion","Clean up orphaned file records whose parent knowledge base no longer exists","Re-create the knowledge base or use an admin cleanup routine to purge stale file records"],"exampleFix":"// before\nawait deleteFileForAgent(agentId, fileId) // 404 if KB was deleted\n// after\nconst file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\nconst kb = file.knowledgeBaseId ? await knowledgeBaseSdk.find(file.knowledgeBaseId) : null\nif (!kb) {\n  // KB gone - purge the orphaned file record via admin/cleanup API instead\n} else {\n  await deleteFileForAgent(agentId, fileId)\n}","handlingStrategy":"validation","validationCode":"const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\nconst kb = file.knowledgeBaseId ? await knowledgeBaseSdk.find(file.knowledgeBaseId) : null\nif (!kb) throw new Error(`Parent knowledge base for ${fileId} no longer exists`)","typeGuard":"const kbExists = async (fileId: string): Promise<boolean> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  return !!file.knowledgeBaseId && !!(await knowledgeBaseSdk.find(file.knowledgeBaseId))\n}","tryCatchPattern":"try {\n  await deleteFileForAgent(agentId, fileId)\n} catch (err) {\n  if (err instanceof HTTPError && err.status === 404) {\n    // parent KB missing - route to orphaned-record cleanup\n  } else throw err\n}","preventionTips":["Cascade-delete file records when removing a knowledge base","Check knowledge base existence before exposing its files in the UI","Periodically sweep for files whose parent KB is gone"],"tags":["knowledge-base","not-found","stale-reference"],"backgroundTag":"parent-resource-not-found","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}