Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete everything from all tables without dropping the database [duplicate]

Possible Duplicate:
Truncating all tables in a postgres database

How can I delete all data from all tables without dropping the database?

like image 550
LeX Avatar asked Dec 12 '22 20:12

LeX


2 Answers

You can use raw connection object to execute SQL statements:

connection = ActiveRecord::Base.connection
connection.tables.each{|t| connection.execute "TRUNCATE #{t}"}
like image 149
Hck Avatar answered Dec 14 '22 10:12

Hck


Use the DatabaseCleaner gem.

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

If you absolutely must have this within a rake task, just wrap it up in one yourself.

like image 36
Will Hamilton Avatar answered Dec 14 '22 10:12

Will Hamilton