Ruby On Rails Active Record Query .delete_all

Example of Ruby On Rails Active Record Query delete_all

If you need to delete a lot of records quickly, ActiveRecord gives .delete_all method. to be called directly on a model, to delete all records in that table, or a collection.

Beware though, as .delete_all does not instantiate any object hence does not provide any callback (before_* and after_destroy don’t get triggered).    

Let’s suppose we have a Blog Model and blogs table which has 50 rows. We want to delete all the blogs records or rows.    

Blog.delete_all    #=> 50  <– .delete_all return the number of rows deleted
Blog.where(author_name: “John”).delete_all

Leave a Reply

Your email address will not be published. Required fields are marked *