Repository navigation
Conversation
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖 |
AlessioGr
enabled auto-merge (squash)
August 27, 2025 22:14
GermanJablo
added a commit
that referenced
this pull request
Sep 2, 2026
## Problem
`payload.delete({ where })` loads the matching documents, then issues
four writes *per document*: a lock cleanup, a version delete, a
scheduled-job delete and a `deleteOne`. Query count grows linearly with
N, which is what exhausts connection pools on large deletes, and turns
one call into hundreds of requests on HTTP-backed adapters. The
`deleteOne` also re-reads a row `db.find()` already loaded, for a result
nobody uses (`returning: false`).
## Change
Those writes are batched behind an `id in [...]` filter, so query count
no longer depends on N. `delete.ts` becomes three phases: per-document
pre-work (locks, `beforeDelete`, file cleanup), batched writes,
per-document post-work (`afterRead`, `afterDelete`). Hooks and
per-document error reporting still run per document; the result shape is
unchanged.
`deleteCollectionVersions` and `deleteScheduledPublishJobs` gain an
optional plural `ids`. Two batched lock helpers sit beside
`checkDocumentLockStatus`, which is itself untouched, so `update`,
`deleteByID` and globals are unaffected.
`bulkOperationsSingleTransaction` (`n: true`) keeps the per-document
path.
## Measured
Query counts from the logger already used by
`test/database/postgres-logs.int.spec.ts`:
| documents | before | after |
| --- | --- | --- |
| 2 | 13 | 8 |
| 20 | 85 | 8 |
| 200 | 805 | 8 |
## Error attribution
Unchanged for every framework-level failure: locks, `beforeDelete`, file
cleanup, `afterRead` and `afterDelete` all still report the exact
document. Access control never produced per-document errors here, since
`access.delete` is merged into the query before `db.find()`, so a denied
document never enters the batch.
Only the collection delete changes: it covers every id at once, so a
failure is reported against the batch with a message saying so, instead
of repeating a driver error against each id as though each failed
independently.
Related: #13618, #15517
---------
Co-authored-by: German Jablonski <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, db.deleteOne will always result in 2 db calls: finding the document, then deleting it.
However, if
returningisfalseand no joins are required for fetching the document we want to delete, we can do this operation in one single db call: delete it while passing the original where query.If
returningisfalsebut joins are required (e.g. due to complex where query referencing other tables), this also slightly optimizes performance by using a narrowerselect.Related PR for deleteMany (already merged - simpler, as deleteMany does not return any documents, deleteOne does): #13255
In order to improve code readability, this also changes this PR to call
this.findOneto find the document instead of manually calling drizzle APIs. This does add minimal overhead (buildQuerybeing called twice), but should not introduce additional db calls.Local API Performance Impact
The speeds up the
payload.delete()local API operation, which usespayload.db.deleteOne+returning: falseinternally. It does not make use ofpayload.db.deleteMany=> Unlike the previous PR optimizing
payload.deleteMany(), this PR will actually have a noticeable, positive performance impact when deleting documents in the admin panel - especially when using bulk delete