How to drop a table in rails?

less than 1 minute read

Drop a Table

Drop users table

$ bin/rails generate migration DropUsers

Generates the following migration:

# db/migrate/20240818074017_drop_users.rb
class DropUsers < ActiveRecord::Migration[7.1]
  def change
  end
end

Add the following code to the migration file to drop the table:

class DropUsers < ActiveRecord::Migration[7.1]
  def change
    drop_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email, null: false

      t.timestamps
    end
  end
end

We don’t necessarily need to specify the columns in the drop_table method. The drop_table method will drop the table with all its columns.

But, the above approach ensures that in case of a rollback, the table will be created with the specified columns.

References

Note

All the commands have been tested with rails 7.1 and the result may differ with prior versions