Paul
Paul

Reputation: 26650

Single step batch delete in Ruby on Rails

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
);

Upvotes: 9

Views: 16025

Answers (2)

Cc Won
Cc Won

Reputation: 1

For me, I use small query to iterate over deletes

while Topic.limit(1000).delete_all > 0 do
  next
end

Upvotes: 0

jimworm
jimworm

Reputation: 2741

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

Upvotes: 9

Related Questions