578

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?

4
  • 1
    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. Sep 12, 2012 at 12:14
  • 5
    @TeemuLeisti How about just copy and paste the current table definition from schema.rb? I do it this way all the time...
    – jasoares
    Aug 31, 2013 at 17:22
  • 1
    @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. Sep 2, 2013 at 9:08
  • 1
    I opened a PR on github for this: github.com/rails/rails/pull/48729 It is not yet reversible (I have same concerns as @TeemuLeisti) but maybe someone else can improve that.
    – knagode
    Jul 13 at 13:36

24 Answers 24

708

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
8
  • 3
    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?
    – berkes
    Jan 17, 2011 at 20:04
  • 3
    Any view here on whether it's better to drop tables or revert to a previous database schema? May 11, 2012 at 16:23
  • 4
    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.
    – Pete
    May 11, 2012 at 21:57
  • 9
    answer by @BederAcostaBorges is more self-explanatory and accurate
    – onerinas
    May 3, 2016 at 6:28
  • 3
    How to also remove all foreign keys? There are columns in other tables pointing to the table being dropped. May 8, 2018 at 16:14
387

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
3
  • 8
    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" Jul 2, 2019 at 20:10
  • 2
    I used the 3rd option and it worked perfectly fine for me. Thank you. Jan 20, 2021 at 18:32
  • Thank You @Beder Acosta Borges I had been wondering how to properly do the migration where it was reversible. In my case it was to remove 2 tables that were not needed. So I basically generated the migration and copied the contents out of the original migration and changed the create_table to drop_table but left the do |t| block with all of the columns and it worked perfectly! TY! Apr 21, 2022 at 17:52
380

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.

6
  • 17
    A down migration should be used to recreate the table being dropped.
    – fflyer05
    Jan 9, 2014 at 1:34
  • 2
    This migration could never be rolled back, even in development. Would it be better to just leave the down migration blank?
    – mhriess
    Apr 4, 2014 at 17:07
  • 1
    This is the better answer + fflyer's comment Jun 15, 2014 at 8:25
  • 2
    @mjnissim and fflyer05 are correct, in order to avoid any weird thing you should recreate the table in the down method. Jan 23, 2015 at 16:43
  • 14
    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.
    – vivi
    Dec 28, 2018 at 15:42
216

Warning: Do this at your own risk, as @z-atef and @nzifnab correctly point out, Rails will not be aware of these changes, your migration sequence fill fail and your schema will be different from your coworkers'. This is meant as a resource for locally tinkering with development only.


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!

4
  • The model is still there until you run rails destroy model User
    – gm2008
    Jul 13, 2014 at 12:29
  • 3
    Only run this if you want to get rid of the table for good. Rails will be unaware of this drop. Migration is broken after running this command. Can not CREATE, DROP...ETC. ERROR SQLite3::SQLException: no such table: accruals: DROP TABLE "sometable"
    – z atef
    Aug 23, 2017 at 14:13
  • WARNING: DO NOT DO THIS. You should never ever perform DDL operations on your database straight from the console. Your structure file will have no idea this change was made, your coworkers development environments will be different from your own and from production, it's just a mess. Use migrations.
    – nzifnab
    Aug 18, 2021 at 18:48
  • Indeed, this should ONLY be done if the table ONLY exists on your local database, no one cares about the data in that table, and the migration has already been deleted and cannot be easily restored. Therefore it won't affect others with access to the repo. IE: The only time I do this if I generated the model, ran migrations then destroyed the scaffold (because of say a typo or something) without reversing the migration first. (faster to just delete the table than try to recover the deleted migration)
    – Nuclearman
    Jun 14, 2022 at 23:31
38

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)
0
24
  1. rails g migration drop_users
  2. edit the migration
    class DropUsers < ActiveRecord::Migration
      def change
        drop_table :users do |t|
          t.string :name
          t.timestamps
        end
      end
    end
  1. rake db:migrate
0
17

The simple and official way would be this:

  rails g migration drop_tablename

Now go to your db/migrate and look for your file which contains the drop_tablename as the filename and edit it to this.

    def change
      drop_table :table_name
    end

Then you need to run

    rake db:migrate 

on your console.

0
15

I wasn't able to make it work with migration script so I went ahead with this solution. Enter rails console using the terminal:

rails c

Type

ActiveRecord::Migration.drop_table(:tablename)

It works well for me. This will remove the previous table. Don't forget to run

rails db:migrate
14

I think, to be completely "official", you would need to create a new migration, and put drop_table in self.up. The self.down method should then contain all the code to recreate the table in full. Presumably that code could just be taken from schema.rb at the time you create the migration.

It seems a little odd, to put in code to create a table you know you aren't going to need anymore, but that would keep all the migration code complete and "official", right?

I just did this for a table I needed to drop, but honestly didn't test the "down" and not sure why I would.

3
  • 1
    Strange but it looks like I'm going to have to do this too. Jul 12, 2011 at 15:20
  • 9
    Or you can just use: raise ActiveRecord::IrreversibleMigration in the self.down method, so you at LEAST give yourself an error / notice if you ever try to rollback.
    – Steph Rose
    Mar 14, 2012 at 15:55
  • 2
    I would test the down just because otherwise I'm introducing untested code into my project. How can I reuse the original migration's up method? I've tried CreateMyTable.up and ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_paths, X) where X is the migration that originally created the table, but neither works--in both approaches, AR first checks whether the migration has already been applied, and silently skips it if it has. ` Jun 5, 2013 at 14:45
14

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

$ rails runner "Util::Table.clobber 'table_name'"
0
13

You can roll back a migration the way it is in the guide:

http://guides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations

Generate a migration:

rails generate migration revert_create_tablename

Write the migration:

require_relative '20121212123456_create_tablename'

class RevertCreateTablename < ActiveRecord::Migration[5.0]
  def change
    revert CreateTablename    
  end
end

This way you can also rollback and can use to revert any migration

12

Alternative to raising exception or attempting to recreate a now empty table - while still enabling migration rollback, redo etc -

def change
  drop_table(:users, force: true) if ActiveRecord::Base.connection.tables.include?('users')
end
9

You can't simply run drop_table :table_name, instead you can create an empty migration by running: rails g migration DropInstalls

You can then add this into that empty migration:

class DropInstalls < ActiveRecord::Migration
  def change
    drop_table :installs
  end
end

Then run rails db:migrate in the command line which should remove the Installs table The solution was found here

1
  • Let me know if you have any q's, will try to answer asap Nov 13, 2021 at 14:19
7

Open you rails console

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

ActiveRecord::Base.connection.drop_table :table_name

1
  • If you are working in a development environment this is a much easier solution than answers with more votes
    – Kai Durai
    Jan 7, 2021 at 14:38
3

if anybody is looking for how to do it in SQL.

type rails dbconsole from terminal

enter password

In console do

USE db_name;

DROP TABLE table_name;

exit

Please dont forget to remove the migration file and table structure from schema

1
  • You can also open it by typing just rails db
    – ARK
    Dec 20, 2019 at 12:36
2

I needed to delete our migration scripts along with the tables themselves ...

class Util::Table < ActiveRecord::Migration

 def self.clobber(table_name)   
    # drop the table
    if ActiveRecord::Base.connection.table_exists? table_name
      puts "\n== " + table_name.upcase.cyan + " ! " 
           << Time.now.strftime("%H:%M:%S").yellow
      drop_table table_name 
    end

    # locate any existing migrations for a table and delete them
    base_folder = File.join(Rails.root.to_s, 'db', 'migrate')
    Dir[File.join(base_folder, '**', '*.rb')].each do |file|
      if file =~ /create_#{table_name}.rb/
        puts "== deleting migration: " + file.cyan + " ! "
             << Time.now.strftime("%H:%M:%S").yellow
        FileUtils.rm_rf(file)
        break
      end
    end
  end

  def self.clobber_all
    # delete every table in the db, along with every corresponding migration 
    ActiveRecord::Base.connection.tables.each {|t| clobber t}
  end

end

from terminal window run:

$ rails runner "Util::Table.clobber 'your_table_name'"

or

$ rails runner "Util::Table.clobber_all"
0
2

Helpful documentation

In migration you can drop table by:

drop_table(table_name, **options)

options:

:force Set to :cascade to drop dependent objects as well. Defaults to false

:if_exists Set to true to only drop the table if it exists. Defaults to false

Example:

  1. Create migration for drop table, for example we are want to drop User table

    rails g migration DropUsers
    
    Running via Spring preloader in process 13189
       invoke  active_record
       create    db/migrate/20211110174028_drop_users.rb
    
  2. Edit migration file, in our case it is db/migrate/20211110174028_drop_users.rb

    class DropUsers < ActiveRecord::Migration[6.1]
      def change
        drop_table :users, if_exist: true
      end
    end
    
  3. Run migration for dropping User table

    rails db:migrate
    
    == 20211110174028 DropUsers: migrating ===============================
    -- drop_table(:users, {:if_exist=>true})
       -> 0.4607s
    
1

the best way you can do is

rails g migration Drop_table_Users

then do the following

rake db:migrate
0
1

Run

rake db:migrate:down VERSION=<version>

Where <version> is the version number of your migration file you want to revert.

Example:-

rake db:migrate:down VERSION=3846656238
-1

Drop Table/Migration

run:- $ rails generate migration DropTablename

exp:- $ rails generate migration DropProducts

0
-1

if you want to drop a specific table you can do

$ rails db:migrate:up VERSION=[Here you can insert timestamp of table]

otherwise if you want to drop all your database you can do

$rails db:drop
-1

Run this command:-

rails g migration drop_table_name

then:

rake db:migrate

or if you are using MySql database then:

  1. login with database
  2. show databases;
  3. show tables;
  4. drop table_name;
4
  • 3
    Does this answer add anything to the existing, accepted answer? If not, there's no need to post this.
    – Tom Lord
    Jun 17, 2016 at 13:27
  • 1
    this creates an empty migration in rails 4.2, as already stated in the question itself. Apr 25, 2017 at 9:40
  • This is EXACTLY what the OP originally tried...this answer should be removed
    – webaholik
    Jun 23, 2018 at 17:52
  • Adding a simple answer does not deserve to be downvoted. It still is relevant, I guess. Be kind to newer users please.
    – ARK
    Dec 20, 2019 at 12:39
-4

If you want to delete the table from the schema perform below operation --

rails db:rollback
1
  • This command will only undo the last migrate command. If that command is the creation of the table, it will drop the table. But unless you then delete the migration file that creates it, it will be recreated the at the next db:migrate. Keeping your migrations in order to replicate elsewhere is important, especially if work has been done in the meantime. Using db:rollback to change the final database layout is almost guaranteed to ruin many people's work.
    – Sean Hogge
    Sep 21, 2020 at 14:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.