How to remove a column using a Rails migration

Updated: 

Learn how to effectively remove a column using a Rails migration in this step-by-step guide from the Full Stack Rails Mastery course.

This lesson is from Full Stack Rails Mastery.

Buy Now
 We've switched to using Action Text for product descriptions. So we no longer need the description field in the products table. We can drop it by creating a Rails migration.

Generate the migration with this command:
rails g migration RemoveDescriptionFromProduct description:text

That will create a migration file under db/migrate with a name as follows:
db/migrate/YYYYMMDD162627_remove_description_from_product.rb

The migration looks like this:
class RemoveDescriptionFromProduct < ActiveRecord::Migration[7.1]
  def change
    remove_column :products, :description, :text
  end
end

Run the migration to drop the column:
bin/rails db:migrate

Note that this is a destructive change - you will lose any data stored in the dropped column. You can technically restore the column with a rollback command:
bin/rails db:rollback 

However, that will only add an empty column back without any data.