{"record":{"id":"83e1781930e0062f","repo":"payloadcms/payload","slug":"not-found","errorCode":null,"errorMessage":"Not Found","messagePattern":"Not Found","errorType":"http","errorClass":"NotFound","httpStatus":404,"severity":"warning","filePath":"packages/payload/src/collections/operations/deleteByID.ts","lineNumber":129,"sourceCode":"\n    let where = combineQueries({ id: { equals: id } }, accessResults)\n\n    // Exclude trashed documents when trash: false\n    where = appendNonTrashedFilter({\n      enableTrash: collectionConfig.trash,\n      trash,\n      where,\n    })\n\n    const docToDelete = await req.payload.db.findOne({\n      collection: collectionConfig.slug,\n      locale: req.locale!,\n      req,\n      where,\n    })\n\n    if (!docToDelete && !hasWhereAccess) {\n      throw new NotFound(req.t)\n    }\n    if (!docToDelete && hasWhereAccess) {\n      throw new Forbidden(req.t)\n    }\n\n    // /////////////////////////////////////\n    // Handle potentially locked documents\n    // /////////////////////////////////////\n\n    await checkDocumentLockStatus({\n      id,\n      collectionSlug: collectionConfig.slug,\n      lockErrorMessage: `Document with ID ${id} is currently locked and cannot be deleted.`,\n      overrideLock,\n      req,\n    })\n\n    await deleteAssociatedFiles({","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/payload/src/collections/operations/deleteByID.ts#L111-L147","documentation":"In `deleteByIDOperation`, after access and query, if `docToDelete` is null and there is no where-access constraint, it throws `NotFound`. The document ID does not exist, was already deleted, or is filtered out by the trash filter.","triggerScenarios":"`payload.deleteByID({ collection, id })` (or `DELETE /api/{collection}/{id}`) with an ID that doesn't exist, was already deleted, or — when trash collection is enabled — is currently trashed while `trash` defaults to false.","commonSituations":"Stale client referencing a deleted doc; a race where another process deleted first; soft-delete/trash enabled so the trashed doc is excluded from the default lookup.","solutions":["Verify the ID exists before deleting, or treat 404 as success for idempotent deletes.","If trash collection is enabled and you mean to delete a trashed doc, pass `trash: true`.","Make delete handlers idempotent by swallowing `NotFound`."],"exampleFix":"// before\nawait payload.deleteByID({ collection: 'posts', id })\n// after\ntry {\n  await payload.deleteByID({ collection: 'posts', id })\n} catch (e) {\n  if (e?.name !== 'NotFound') throw e\n  // already gone — idempotent success\n}","handlingStrategy":"try-catch","validationCode":"async function docExists(payload, slug, id) {\n  const doc = await payload.findByID({ collection: slug, id, disableErrors: true, overrideAccess: true })\n  return !!doc\n}","typeGuard":"function isNotFound(e): boolean {\n  return e?.statusCode === 404 || e?.name === 'NotFound'\n}","tryCatchPattern":"try {\n  await payload.deleteByID({ collection, id })\n} catch (e) {\n  if (isNotFound(e)) return { ok: true, alreadyDeleted: true }\n  throw e\n}","preventionTips":["Make deletes idempotent by swallowing `NotFound`.","Check existence only if you must distinguish missing docs from success."],"tags":["delete","not-found","idempotency"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}