Using ActiveRecord find_in_batches method for deleting large data

New functionality: #in_batches (https://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-in_batches)

Person.where("age > 21").in_batches do |relation|
  relation.delete_all
end

I don't think anyone has answered your question.

To answer 'what you are doing wrong' and can you use 'find_in_batches' in that way:

The reason why 'delete_all' does not work, is because 'delete_all' only works on activerecord relations. When you use 'find_in_batches' the variable 'batch' is now just a normal array, which may have it's own 'delete_all' method that is different,

You may need 'find_in_batches' incase if you have thousands of records to be deleted. So the previous answer is incorrect. (It may lead to memory exceeded exceptions and timeouts)

Note that is not related to the original error you displayed, but you cannot use 'batch' with 'delete_all' because 'batch' is an array and 'delete_all' is for activerecords

How to delete using find_in_batches

I was having a similar problem

user.posts.destroy_all

was overloading the server because of thousands of posts (this is an example my actual model was not 'posts')

You can use

user.posts.select(:id).find_in_batches(batch_size: 100) do |ids|
  Post.where(id: ids).delete_all
end

If it was one sql call, it will try to store all the delete items in memory at once that can break the server, This will have a manageable size of sql calls.


Use Model.in_batches.destroy_all.

Be mindful of dependent associations which might still be loaded without batching. I created a gem to fix this: batch_dependent_associations

See also dhh's issue in Rails: Relation#destroy_all should perform its work in batches