Menu

bulk_destroyer: destroy_all that fires every callback, without one DELETE per row
πŸ“°

bulk_destroyer: destroy_all that fires every callback, without one DELETE per row

DEV Community: rubyΒ·RamananΒ·5 days ago
#5XEgHyOo
#devtoruby#devto#dev#runs#destroy
Reading 0:00
15s threshold

A Rails gem that destroys records and their dependent: :destroy trees with set-based SQL while still running every destroy callback, including the children's.

delete_all is fast but skips callbacks. destroy_all runs every callback but loads each record and deletes it with its own query, and does the same for every child. On a tree with a few thousand parents and tens of thousands of children, that is tens of thousands of queries.

I wanted both: every before_destroy , around_destroy and after_destroy hook, on every record in the tree, with bulk SQL underneath. So I built bulk_destroyer .

Post . where ( author_id: 42 ). bulk_destroy_all post . bulk_destroy!

Enter fullscreen mode

Exit fullscreen mode

What you get

Per batch, it issues one SELECT per association per level and one DELETE per table per level.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.