Laravel comes with many convenient tools out of the box, which makes your life as a developer much more enjoyable.

One of the best Laravel features is the database migrations which essentially allow you to version control your database! In this tutorial, you will learn how to rollback your database migrations in Laravel if you ever have to do so!

Rollback the last database migration

Let's say that you've run a database migration but then realized that you should not have done so. Laravel makes it easy to revert/rollback your last migration. To do so, you could just run the following command:

php artisan migrate:rollback --step=1

In case that you need to revert multiple migrations, you could change the --step=1 with the number of migrations that you want to rollback.

If you don't specify the --step= flag, and just run the following:

php artisan migrate:rollback

This would rollback your last batch of migrations. So let's say that you've added 5 migration files and run php artisan migrate to run all 5 migrations at once. This would be considered as 1 batch.

Rollback specific migration

In other cases, you might want to rollback just a specific migrations file rather than the last one. You can do that with the --path flag followed by the path to the migrations file.

Example:

php artisan migrate:rollback --path=/database/migrations/the_specific_migration_file.php

This could be particularly handy if you want to make a minor change in your local dev environment, but you need to be careful with table drops and foreign keys.

Rollback all migrations

If you wanted to rollback all of your migrations, you could run the following:

php artisan migrate:reset

Note that you should never do that in a production environment as you will lose all of your data.

The above would essentially truncate your database. In case that you wanted to drop all tables and then run the migrations from scratch, you could use the following command:

php artisan migrate:refresh

That way, you will get a fresh new database with empty tables.