TIL: Truncating tables in Rails

Posted on

Today I learned that you can truncate tables in Rails.

1
ActiveRecord::Base.connection.truncate(Model.table_name)

Quicker than Model.delete_all if you are clearing out a large table. The trade-off: truncate bypasses Rails callbacks and resets the auto-increment counter, so it’s only appropriate when you genuinely want to wipe the table and don’t need dependent deletions or validations to fire.