Rails 4: How to reset test database?

An overkill solution would be:

bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:schema:load RAILS_ENV=test

You could make this all in a rake task and run that.

Another solution from here is to include the following your spec_helper.rb file

config.after :all do
  ActiveRecord::Base.subclasses.each(&:delete_all)
end

Disclaimer: I have not tested this and you should read the SO post as it may not work in all situations.

That being said, I would recommend using the database cleaner gem to avoid situations such as this.


It can be:

(For rails Rails 5+)

bundle exec rails db:reset RAILS_ENV=test

For previous versions

bundle exec rake db:reset RAILS_ENV=test

Sometimes you might need to run this command (optional)

rails db:environment:set RAILS_ENV=test

But for sure to wipe out your test database should be as easy as:

rails db:drop db:create db:migrate RAILS_ENV=test


You can add an after filter deleting all the entries from the concerned tables.