Deletes/destroys associated records in the background, because destroying a billion records in the foreground just will not work.
Because you probably have destroy callbacks and those are the devil. 😈🔱
Add this line to your application's Gemfile:
gem 'delete_you_later'
And then execute:
$ bundle
class Post < ApplicationRecord
has_many :posts
has_many :likes
destroy_dependents_later :posts
delete_dependents_later :likes
end
Drop into an initializer:
DeleteYouLater.configure do |config|
config.scope = :published # default is not scoped
config.batch_size = 100_000 # default is 100
end
You can change these at runtime:
class Post < ApplicationRecord
has_many :comments
destroy_dependents_later :comments, batch_size: 10, scope: :unapproved
end
This isn't one-size-fits-all but one-size-fits-joshmn's-clients. It might help you too, if not at least give you inspiration for modeling this in your application. I've used this for billions of records with success. Don't ask about my failures.
Bug reports and pull requests are kindly welcomed on GitHub at https://github.com/joshmn/delete_you_later
The gem is available as open source under the terms of the MIT License.