Adding columns or tables to your database manually could be an intimidating process and would more often than not lead to database inconsistencies between your different environments. The Laravel migrations allow you to basically version control your database so that all members of your team could have a consistent database schema.

In this tutorial, you will learn how to remove a migration for your Laravel application!

Creating a Laravel migration

In order to create a Laravel migration, you would use the following artisan command:

php artisan make:migration create_videos_table

The naming convention when creating new tables is the following:

  • Start with the create keyword
  • Followed by the name of the table, in our example this is videos
  • Followed by the table keyword as we are adding a new table.

Once you run the command, a new file will be created at:

database/migrations/the_name_of_your_migration_file.php

Now that we've got the migration creation covered, let's see how we could actually remove a migration.

Remove a migration in Laravel

We've got the php artisan make:migration to make migrations, but there is no command for removing them. To do so, you would need to actually delete the migrations file.

Let's cover two cases:

Remove a migration that hasn't been executed yet

If you've only created the migration and you've not executed the php artisan migrate command, all you would need to do to remove the migration is to delete the file.

You could do that via your text editor or the command line with the rm command.

First check if the migration has been executed yet, you could use the following command:

C:\xampp\htdocs\laravel-test.com>php artisan migrate:status
+------+-------------------------------------------------------+-------+
| Ran? | Migration                                             | Batch |
+------+-------------------------------------------------------+-------+
| Yes  | 2014_10_12_000000_create_users_table                  | 1     |
| Yes  | 2014_10_12_100000_create_password_resets_table        | 1     |
| Yes  | 2019_08_19_000000_create_failed_jobs_table            | 1     |
| Yes  | 2019_12_14_000001_create_personal_access_tokens_table | 1     |
| No   | 2021_09_06_074238_create_videos_table                 |       |
+------+-------------------------------------------------------+-------+

If the migration has not been ran yet, remove the file:

database/migrations/the_name_of_your_migration_file.php

Removing a migration that has already been executed

In case that you've ran the migration already, in order to revert it, you could use the following command:

php artisan migrate:rollback --step=1

This will revert only the last migration. After that, you could again use the rm command as described in the past video to actually remove the migrations file.

Reverting all migrations (DEV environments only)

Alternatively, if you are on a local dev environment and you don't actually need the data in the database, you could run migrate:fresh to actually revert all migrations and then run them again.

php artisan migrate:fresh

Note: if you run migrate fresh, this will wipe all of your data, so you need to be careful with that!