Alex Rivera | Logout

How to delete all data from all tables in Rails?

Asked 2009-07-28T19:14:49.300
70

I can do Post.delete_all to delete all my posts, but what if I want to delete all posts, comments, blogs, etc.?

How do I iterate over all my models and run the delete_all method?

Edit
Report

1 Answer

4

You could list all the models in the seed-file (seeds.rb), and simply run

rake db:seed

The seed file would then look something like this:

Model1.delete_all
Model2.delete_all
Model3.delete_all
Model4.delete_all
Model5.delete_all
Model6.delete_all
Model7.delete_all

...

rake db:reset is too much for your job here. That will completely kill off your database and rebuild it from scratch, running all migrations etc. To run the seed command is faster.

answered 2012-07-06T19:40:59.140

Your Answer