How to empty a Heroku database

user591338 picture user591338 · Jan 27, 2011 · Viewed 146.8k times · Source

I'm working on a Ruby on Rails 3 webapp on Heroku. How do I empty the database?

Answer

Shaun picture Shaun · Jan 27, 2011

To drop the database, if you are using SHARED_DATABASE_URL:

$ heroku pg:reset DATABASE_URL

Now to recreate the database with nothing in it:

$ heroku run rake db:migrate  

To populate the database with your seed data:

$ heroku run rake db:seed

---OR---

You can combine the last two (migrate & seed) into one action by executing this:

$ heroku run rake db:setup

Edit 2014-04-18: rake db:setup doesn't work with Rails 4, it fails with a Couldn't create database error.

Edit 2014-10-09: You can use rake db:setup with Rails 4. It does give you a Couldn't create database error (because the database was already created using the heroku pg:reset command). But it also loads your database schema and your seeds after the error message.

You can do this with pretty much any rake command, but there are exceptions. For example, db:reset doesn't work via heroku run rake. You have to use pg:reset instead.

More information can be found in Heroku's documentation:

Running Rake Commands

Reset Postgres DB