Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions packages/payload/src/collections/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const deleteOperation = async <

const errors: BulkOperationResult<TSlug, TSelect>['errors'] = []

const promises = docs.map(async (doc) => {
const processDoc = async (doc: (typeof docs)[number]) => {
let result

const { id } = doc
Expand Down Expand Up @@ -307,18 +307,17 @@ export const deleteOperation = async <
})
}
return null
})
}

// Process sequentially when using single transaction mode to avoid shared state issues
// Process in parallel when using one transaction for better performance
let awaitedDocs
if (req.payload.db.bulkOperationsSingleTransaction) {
awaitedDocs = []
for (const promise of promises) {
awaitedDocs.push(await promise)
}
} else {
awaitedDocs = await Promise.all(promises)
// Process documents sequentially to prevent concurrent deleteOne calls
// from interleaving queries on a shared transaction connection.
// Using .map(async ...) would eagerly start all callbacks, causing
// silent data loss even with for...of await (which only controls the
// order results are collected, not execution order).
// See: https://github.com/payloadcms/payload/issues/16075
const awaitedDocs = []
for (const doc of docs) {
awaitedDocs.push(await processDoc(doc))
}

// /////////////////////////////////////
Expand Down
Loading