Skip to content

Conversation

@AlessioGr
Copy link
Member

@AlessioGr AlessioGr commented Aug 27, 2025

Currently, db.deleteOne will always result in 2 db calls: finding the document, then deleting it.

However, if returning is false and 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 returning is false but joins are required (e.g. due to complex where query referencing other tables), this also slightly optimizes performance by using a narrower select.

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.findOne to find the document instead of manually calling drizzle APIs. This does add minimal overhead (buildQuery being called twice), but should not introduce additional db calls.

Local API Performance Impact

The speeds up the payload.delete() local API operation, which uses payload.db.deleteOne + returning: false internally. It does not make use of payload.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


@github-actions
Copy link
Contributor

github-actions bot commented Aug 27, 2025

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@AlessioGr AlessioGr changed the title perf(drizzle): 2x faster db.deleteOne if not returning perf(drizzle): 2x faster document deletion Aug 27, 2025
@AlessioGr AlessioGr enabled auto-merge (squash) August 27, 2025 22:14
@AlessioGr AlessioGr requested a review from r1tsuu September 5, 2025 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant