Post

How to Drop Rails Table

How to Drop Rails Table

Note: Dropping a table will remove all the data from it.

I recently had to remove a table from a Rails application. Here are the ways to do it.

Using a Migration

Why this option?

  • It’s a standard way to make database changes
  • It’s reversible, so you can rollback if needed (though data won’t be recovered)
1
rails generate migration DropUsers
1
2
3
4
5
6
7
8
9
class DropUsers < ActiveRecord::Migration[7.0]
  def change
    drop_table :users do |t|
      t.string :name
      t.string :email
      t.timestamps
    end
  end
end

I saw some blog posts that only use drop_table :users, but that’s not reversible. You may see this error when rolling back:

1
2
3
4
5
6
7
8
To avoid mistakes, drop_table is only reversible if given options or a block (can be empty).

doctors/db/migrate/20240228054356_drop_users.rb:3:in `change'

Caused by:
ActiveRecord::IrreversibleMigration:

To avoid mistakes, drop_table is only reversible if given options or a block (can be empty).

Use a block to avoid the error.

Using the Rails Console

Why this option?

  • You don’t want to track the change history
1
2
3
4
5
6
bin/rails console
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS users;")

# or using the helper method:

ActiveRecord::Migration.drop_table :users

Using a Database Client

Why this option?

  • You don’t care about change history
  • You don’t have access to run Rails commands

Depending on the database you are using, you can use the client to drop the table.

For example, using the psql client for PostgreSQL:

1
2
psql -U username -d database_name
DROP TABLE IF EXISTS users;

Here you can see the pull request where I dropped the table.

This post is licensed under CC BY 4.0 by the author.