Skip to content

ankane/delete_in_batches

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

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

delete_in_batches

🔥 Fast batch deletes for Active Record and Postgres

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'delete_in_batches'

How to Use

Delete rows in batches

Tweet.where(user_id: 1).delete_in_batches

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

Sleep between batches

Tweet.where(user_id: 1).delete_in_batches(sleep: 0.01)

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

Note: This project originally had the description “the fastest way to delete 100k+ rows with ActiveRecord” but a single DELETE statement will likely be faster. See this discussion for more details.

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

About

Fast batch deletes for Active Record and Postgres

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages