Read more

Delete all MySQL records while keeping the database schema

Henning Koch
March 14, 2011Software engineer at makandra GmbH

You will occasionally need to clean out your database while keeping the schema intact, e.g. when someone inserted data in a migration or when you had to kill -9 a frozen test process.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Old Capybara versions already have the Database Cleaner gem Show archive.org snapshot as dependency. Otherwise add database_cleaner to your *Gemfile`. This lets you say this from the Rails console:

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

You can package this into a nice little Rake task by copying the attachment into lib/tasks/clean_database.rake. Now you can e.g. clean the test database by saying on the shell:

rake db:clean RAILS_ENV=test

If you are using the parallel_tests gem, the task is aware and will clean all your test databases.

Posted by Henning Koch to makandra dev (2011-03-14 11:08)