Rails DB Migration - How To Drop a Table?

rails drop table if exists
rails drop table command line
rails db:drop
rails db:migrate
rails delete table from schema
rails delete migration
rails migration up down
rails migration null: false

I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?

I've already run migrations, so the table is in my database. I figure rails generate migration should be able to handle this, but I haven't figured out how yet.

I've tried:

rails generate migration drop_tablename

but that just generated an empty migration.

What is the "official" way to drop a table in Rails?

You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.

You can find information about how to accomplish different tasks in a migration here:

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

More specifically, you can see how to drop a table using the following approach:

drop_table :table_name

Rails Migrations Tricks [Guide + Code], The methods Active Record provides to manipulate your database. The rails commands that manipulate migrations and your schema. starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Rails 3.1 makes migrations smarter by providing a new change method. This method is preferred for writing constructive migrations (adding columns or tables). The migration knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method.

First generate an empty migration with any name you'd like. It's important to do it this way since it creates the appropriate date.

rails generate migration DropProductsTable

This will generate a .rb file in /db/migrate/ like 20111015185025_drop_products_table.rb

Now edit that file to look like this:

class DropProductsTable < ActiveRecord::Migration
  def up
    drop_table :products
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end

The only thing I added was drop_table :products and raise ActiveRecord::IrreversibleMigration.

Then run rake db:migrate and it'll drop the table for you.

Active Record Migrations, Command Line. Drop Table/Migration. rails generate migration DropTablename. A file will be created, in the db > migrate folder, make sure it looks like: class DropUsers < ActiveRecord::Migration def change drop_table :users end end. Drop Model. rails d model user. Drop Controller. rails d controller users. Re-Migrate. rake db:drop rake db:create rake db:migrate. Or just rake db:reset, which will run the above steps but will also run your db/seeds.rb file. An added nuance is that rake db:reset loads directly from your schema.rb file as opposed to running all the migrations files again. You data gets blown away in all cases.

Write your migration manually. E.g. run rails g migration DropUsers.

As for the code of the migration I'm just gonna quote Maxwell Holder's post Rails Migration Checklist

BAD - running rake db:migrate and then rake db:rollback will fail
class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users
  end
end
GOOD - reveals intent that migration should not be reversible
class DropUsers < ActiveRecord::Migration
  def up
    drop_table :users
  end

  def down
    fail ActiveRecord::IrreversibleMigration
  end
end
BETTER - is actually reversible
class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users do |t|
      t.string :email, null: false
      t.timestamps null: false
    end
  end
end

Drop Table, Model, Migration in Rails · GitHub, How to Remove Old Database Migrations in Ruby on Rails tables for models and also a special table schema_migrations to track migrations. If you have already run your original migration (before editing it), then you need to generate a new migration (rails generate migration add_email_to_users email:string will do the trick). It will create a migration file containing line: add_column :users, email, string Then do a rake db:migrate and it'll run the new migration, creating the new column.

While the answers provided here work properly, I wanted something a bit more 'straightforward', I found it here: link First enter rails console:

$rails console

Then just type:

ActiveRecord::Migration.drop_table(:table_name)

And done, worked for me!

How to Remove Old Database Migrations in Ruby on Rails, Ruby on Rails latest stable (v5.2.3) - 1 note - Class: if you want to drop a table before creating one in a migration, use the :force => true option of the  The problem is when a migration is run, rails adds the version into a schema_migrations table in the database. So even if it isn't listed in your db structure or schema, rails looks for it. So even if it isn't listed in your db structure or schema, rails looks for it.

You need to to create a new migration file using following command

rails generate migration drop_table_xyz

and write drop_table code in newly generated migration file (db/migration/xxxxxxx_drop_table_xyz) like

drop_table :tablename

Or if you wanted to drop table without migration, simply open rails console by

$ rails c

and execute following command

ActiveRecord::Base.connection.execute("drop table table_name")

or you can use more simplified command

ActiveRecord::Migration.drop_table(:table_name)

drop_table (ActiveRecord::ConnectionAdapters::SchemaStatements , I just use the self.down method of origin migration that creates the table to drop down the table but the command 'rake db:migrate' not works. If you want to rollback a specific migration with a version you should do: rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION If the migration file you want to rollback was called db/migrate/20141201122027_create_some_table.rb , then the VERSION for that migration is 20141201122027 , which is the timestamp of when that migration was created

Remove a table with migration - Rails, Update the migration timestamp. ## Dropping rake db:drop Sometimes we want to delete all of the data and tables and start from fresh. That's  There is a table in your database called schema_migrations that has a single varchar(255) column called version; this table is used by db:migrate to keep track of which migrations have been applied. All you need to do is INSERT the appropriate version value and rake db:migrate will think that the migration has been done.

How to use rake db commands in the correct way, When dropping a database table in a migration all data contained within can run rails db:migrate followed by rails db:rollback and recover the  you can simply drop a table from rails console. first open the console $ rails c then paste this command in console. ActiveRecord::Migration.drop_table(:table_name) replace table_name with the table you want to delete. you can also drop table directly from the terminal. just enter in the root directory of your application and run this command

Ensure dropping a database table is reversible, To revert our change is simple, you need only drop the table you created in the up definition. script/generate migration SampleMigration create db/migrate create Rails Migrations transparently adds a SCHEMA_INFO table to keep track of 

Comments
  • Since rails generate migration has command-line options for generating migration code for creating tables, adding or changing columns, etc., it would be nice if it also had an option for dropping a table -- but it doesn't. Sure, writing the up part is simple -- just call drop_table -- but the down part, generating the table again, might not always be so simple, especially if the schema of the table in question has been changed by migrations after its initial creation. Maybe someone should suggest to the developers of Rails that adding such an option would be a good idea.
  • @TeemuLeisti How about just copy and paste the current table definition from schema.rb? I do it this way all the time...
  • @João Soares: OK, I guess that works. However, it would be nice if the process could be automated, so that you could just give a rake migration-creation command, with the name of a table as a parameter, that would produce the needed up and down functions.
  • This worked for me too. But on full migrations (installing from scratch) the table will now be first created and later on dropped again. Is it safe to remove the create and drop migrations down the road?
  • Any view here on whether it's better to drop tables or revert to a previous database schema?
  • If you're done with the table and do not plan to use it anymore, I'd say just drop it. Better to get rid of it if its not being used.
  • answer by @BederAcostaBorges is more self-explanatory and accurate
  • How to also remove all foreign keys? There are columns in other tables pointing to the table being dropped.
  • A down migration should be used to recreate the table being dropped.
  • This migration could never be rolled back, even in development. Would it be better to just leave the down migration blank?
  • This is the better answer + fflyer's comment
  • @mjnissim and fflyer05 are correct, in order to avoid any weird thing you should recreate the table in the down method.
  • Dropping a table deletes all the data, if you recreate it in the down method you won't recover it so it's not actually a proper roll back. It's better to clearly indicate that the migration is irreversible than to give a false sense that it can be recovered from.
  • If you're cutting and pasting into the block from schema.rb, don't forget to also search schema.rb for foreign keys. Then add the foreign key definition to the drop_table block, e.g.: t.foreign_key "other_table"