How to remove index in rails

From rails console, run following

ActiveRecord::Migration.remove_index "completions", name: "index_completions_on_survey_id_and_user_id"

You don't supply the columns in the index when removing one. Try:

remove_index :completions, name: "index_completions_on_survey_id_and_user_id"

The accepted answer here doesn't work when you need to roll back the migration, it will be given ActiveRecord::IrreversibleMigration error.

remove_index is only reversible if given a :column option.

def change
  remove_index "completions", column: [:survey_id], name: "index_completions_on_survey_id_and_user_id"
end

this will remove the index and also reversible.


You can supply the column name(s) to remove_index. The remove_index method takes table_name and options as parameters. With the options passed in the name of the index is determined via index_name_for_remove private method, which simply does (if it is an array):

...
column_names = Array(options).map(&:to_s)
...

if column_names.any?
  checks << lambda { |i| i.columns.join('_and_') == column_names.join('_and_') }
end

Examples from the API documentation:

Removes the index on branch_id in the accounts table if exactly one such index exists.

remove_index :accounts, :branch_id

Or

remove_index :accounts, column: :branch_id

Removes the index on branch_id and party_id in the accounts table if exactly one such index exists.

remove_index :accounts, column: [:branch_id, :party_id]

Removes the index named by_branch_party in the accounts table.

remove_index :accounts, name: :by_branch_party

Apart from the above, you could just do:

remove_index :accounts, %i[branch_id party_id]