How to rollback N numbers of migration in rails?

less than 1 minute read

Sometimes, We need to rollback a specific number of migrations to undo series of changes.

To rollback n numbers of migrations, you can use the db:rollback command along with the STEP option, specifying the number of migrations to rollback.

For example, to rollback the last 3 migrations, you can run:

# RAILS >= 5.0
rails db:rollback STEP=3

# RAILS < 5.0
rake db:rollback STEP=3

This command will revert the last 3 migrations, undoing their changes to the database schema and data.

This is useful for reverting accidental changes, testing data migrations, or resetting the database to a previous state.

References