Skip to content
master
Go to file
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

delete_in_batches

🔥 The fastest way to delete 100k+ rows with ActiveRecord

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'delete_in_batches'

Slow

Tweet.where(user_id: 1).delete_all
# DELETE FROM tweets WHERE user_id = 1

Faster

Tweet.where(user_id: 1).in_batches(of: 10000).delete_all
# SELECT tweets.id FROM tweets WHERE user_id = 1 ORDER BY id LIMIT 1000
# DELETE FROM tweets WHERE user_id = 1 AND id IN (1, 2, 3, ...)
# ...

Fastest

Tweet.where(user_id: 1).delete_in_batches
# DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
# DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
# ...

Important: Be sure to test your query before running it in production

Change the batch size

Tweet.where(user_id: 1).delete_in_batches(batch_size: 50000) # defaults to 10000

Show progress

Tweet.where(user_id: 1).delete_in_batches do
  puts "Another batch deleted"
end

Works with associations

user.tweets.delete_in_batches

To delete all rows in a table, TRUNCATE is fastest.

ActiveRecord::Base.connection.execute("TRUNCATE tweets")

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/delete_in_batches.git
cd delete_in_batches
bundle install
bundle exec rake test
You can’t perform that action at this time.