findEach
The findEach method will collect records in batches of 1000 by default (though you can customize the batch size with a second argument), and then will proceed to call your callback function for each record found:
await User.findEach(async (user) => {
if (user.email.includes('burpcollaborator')) await user.destroy()
})
await User.findEach(
async (user) => {
if (user.email.includes('burpcollaborator')) await user.destroy()
},
{ batchSize: 20000 },
)
Chaining
Similar to other execution methods, findEach can be chained with other chainable query methods, such as where and whereNot:
await User.whereNot({ email: null }).findEach(async (user) => {
if (user.email.includes('burpcollaborator')) await user.destroy()
})
findEach visits each row that all() would return exactly once. On a query that joins a HasMany, that is once per joined row, so Place.innerJoin('rooms').findEach(...) visits each place once per room.
A query that carries a limit or offset throws BatchingIncompatibleWithLimitOrOffset, since batching would re-apply either inside every batch rather than bounding the run as a whole.
Because it processes records in batches by primary key, findEach always visits matched records in ascending primary-key order and ignores any order applied to the query. This is also why a default (non-skipHooks) query.update call — which goes through findEach internally — can't honor an arbitrary order either.