fix(drizzle): process bulk deletes sequentially to prevent silent data loss#16076
Open
razmenaia wants to merge 1 commit intopayloadcms:mainfrom
Open
fix(drizzle): process bulk deletes sequentially to prevent silent data loss#16076razmenaia wants to merge 1 commit intopayloadcms:mainfrom
razmenaia wants to merge 1 commit intopayloadcms:mainfrom
Conversation
… loss The bulk delete operation used .map(async ...) to create promises for each document, then either Promise.all or for...of await. However, .map(async ...) eagerly starts all callbacks immediately -- the for...of await loop only controls the order results are collected, not execution order. This caused concurrent deleteOne calls on a shared transaction connection, where interleaved queries silently dropped deletes. Replace with a proper sequential for...of loop that awaits each document deletion before starting the next. Reproduces consistently on Payload 3.80.0 with @payloadcms/db-postgres. Verified not present in 3.41.0 (regression). Fixes payloadcms#16075 Related: payloadcms#15100 Made-with: Cursor
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.
Summary
Fixes #16075
payload.delete({ where })silently fails to delete 1+ records. Payload reports success and returns the correct IDs, but the records remain in the database.Root Cause
The bulk delete operation in
delete.tsuses.map(async ...)to create per-document promises, then eitherPromise.allorfor...of awaitto process them. However,.map(async ...)eagerly starts all async callbacks immediately —for...of awaitonly controls the order results are collected, not execution order.This means all
deleteOnecalls run concurrently on a shared transaction connection. EachdeleteOneperforms 3 queries (selectDistinct→findFirst→deleteWhere) that interleave and silently drop deletes.bulkOperationsSingleTransaction: truedoes not fix it because of the same.map()antipattern.Fix
Replace
.map(async ...)+for...of awaitwith a proper sequentialfor...ofloop:Reproduction
@payloadcms/db-postgresdb.delete().where()works perfectly — confirms the bug is in Payload's abstraction layer, not Drizzle or PostgresSee #16075 for full details, POC scripts, and proof.
Made with Cursor