Ruby on Rails: 3 tips for deleting data at scale
Aug 1, 2022Mike Coutermarsh

Summary
This post provides three tips for safely deleting data at scale in Rails applications: 1. Use `dependent: :destroy_async` to move deletions to background jobs, preventing request timeouts and database strain, and recommending replacing foreign key `ON DELETE CASCADE` with this approach. It also warns about potential validation failures with `destroy_async`. 2. Differentiates between `delete` (skips callbacks) and `destroy` (triggers callbacks), advising `delete_all` for bulk cleanup to skip callbacks. 3. Recommends batching mass deletions of old data using a Sidekiq job with a `limit` and self-requeuing mechanism to avoid locking tables and impacting application performance. It also provides testing examples for these jobs.