Deleting all records in a database table

Justin Meltzer picture Justin Meltzer · Mar 16, 2011 · Viewed 101.2k times · Source

How do I delete all records in one of my database tables in a Ruby on Rails app?

Answer

HakonB picture HakonB · Mar 16, 2011

If you are looking for a way to it without SQL you should be able to use delete_all.

Post.delete_all

or with a criteria

Post.delete_all "person_id = 5 AND (category = 'Something' OR category = 'Else')"

See here for more information.

The records are deleted without loading them first which makes it very fast but will break functionality like counter cache that depends on rails code to be executed upon deletion.