ActiveRecord slow_delete_all

Deletes taking to long, just take a break 🙂
(so replication can catch up/things get unblocked)

Only use save sql, there is no escapeing.

  class ActiveRecord::Base
    def self.slow_delete_all(condition)
      count = 0
      loop do
        result = ActiveRecord::Base.connection.send(:delete_sql, "delete from #{table_name} where #{condition} limit 10000")
        count += result
        break if result == 0
        sleep 1
      end
      count
    end
  end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s