Single step batch delete in Ruby on Rails
Asked Answered
B

2

9

How to send such a query to database server from Rake task without removing record-by-record in "each" loop?

delete from data
where uuid in (
    select uuid
    from data
    group by uuid, raw, recdate
    having count(*)>1
);
Blotto answered 15/5, 2012 at 10:33 Comment(0)
Q
9

ActiveRecord has the delete_all method for you. Note that it does not call the destroy callbacks. http://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all

Quagga answered 15/5, 2012 at 11:57 Comment(2)
Is there a rails console function which calls destroy callbacks when deleting parents?Bedsore
.destroy_all does it but that is not a single-step batch delete the question asked for.Quagga
R
0

For me, I use small query to iterate over deletes

while Topic.limit(1000).delete_all > 0 do
  next
end
Redhot answered 21/6, 2022 at 2:1 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Neurath

© 2022 - 2024 — McMap. All rights reserved.