Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ankane/delete_in_batches
Fast batch deletes for Active Record and Postgres
https://github.com/ankane/delete_in_batches
Last synced: 3 months ago
JSON representation
Fast batch deletes for Active Record and Postgres
- Host: GitHub
- URL: https://github.com/ankane/delete_in_batches
- Owner: ankane
- License: mit
- Archived: true
- Created: 2014-01-28T05:01:24.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T17:52:33.000Z (almost 2 years ago)
- Last Synced: 2024-07-21T00:50:36.793Z (4 months ago)
- Language: Ruby
- Homepage:
- Size: 29.3 KB
- Stars: 200
- Watchers: 7
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# delete_in_batches
:fire: Fast batch deletes for Active Record and Postgres
[![Build Status](https://github.com/ankane/delete_in_batches/workflows/build/badge.svg?branch=master)](https://github.com/ankane/delete_in_batches/actions)
## Installation
Add this line to your application’s Gemfile:
```ruby
gem 'delete_in_batches'
```## How to Use
Delete rows in batches
```ruby
Tweet.where(user_id: 1).delete_in_batches
```**Important:** Be sure to test your query before running it in production
Change the batch size
```ruby
Tweet.where(user_id: 1).delete_in_batches(batch_size: 50000) # defaults to 10000
```Sleep between batches
```ruby
Tweet.where(user_id: 1).delete_in_batches(sleep: 0.01)
```Show progress
```ruby
Tweet.where(user_id: 1).delete_in_batches do
puts "Another batch deleted"
end
```Works with associations
```ruby
user.tweets.delete_in_batches
```To delete all rows in a table, `TRUNCATE` is fastest.
```ruby
ActiveRecord::Base.connection.execute("TRUNCATE tweets")
```## History
View the [changelog](https://github.com/ankane/delete_in_batches/blob/master/CHANGELOG.md)
**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](https://github.com/ankane/delete_in_batches/issues/4) for more details.
## Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- [Report bugs](https://github.com/ankane/delete_in_batches/issues)
- Fix bugs and [submit pull requests](https://github.com/ankane/delete_in_batches/pulls)
- Write, clarify, or fix documentation
- Suggest or add new featuresTo get started with development:
```sh
git clone https://github.com/ankane/delete_in_batches.git
cd delete_in_batches
bundle install
bundle exec rake test
```