[laravel] Rollback one specific migration in Laravel

I want

to rollback only :

Rolled back: 2015_05_15_195423_alter_table_web_directories


I run

php artisan migrate:rollback, 3 of my migration are rolling back.

Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_13_135240_create_web_directories_table
Rolled back: 2015_05_13_134411_create_contacts_table

I delete

both of my web_directories and my contacts table unintentionally. I never want that to happen, and if I can rollback only that specific one, this disaster will never happen.

This question is related to laravel laravel-4 laravel-5 database-migration

The answer is


Best way is to create a new migration and make required changes in that.

Worst case workaround (if you have access to DB plus you are okay with a RESET of that table's data):

  1. Go to DB and delete/rename the migration entry for your-specific-migration
  2. Drop the table created by your-specific-migration
  3. Run php artisan migrate --path=/database/migrations/your-specific-migration.php

This will force laravel to run that specific migration as no entry about it exists in Laravel's migration history

UPDATE: The Laravel way (Thanks, @thiago-valente)

Run:

php artisan migrate:rollback --path=/database/migrations/your-specific-migration.php

and then:

php artisan migrate

This will re-run that particular migration


Migrate tables one by one.

Change the batch number of the migration you want to rollback to the highest.

Run migrate:rollback.

May not be the most comfortable way to deal with larger projects.


If you have access to the DB you have a easier solution. You can delete the record from migrations table and then just drop the table. with SQL client.

And Can use

php artisan migrate:rollback --path=... 

Like many answers. And remember the path is Location. You can remove even module migration like this. (Any migration from anywhare)

php artisan migrate:rollback --path=Modules/YourModule/database/migrations/2020_05_15_xxxxxx_create_your_table.php

And remember, If you are using linux servers careful about case sensitivity. You have to add like /Database/Migrations with starting capital.

/Database/Migrations/2020_05_15_xxxxxx_create_your_table.php

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

1.) Inside the database, head to the migrations table and delete the entry of the migration related to the table you want to drop.

Migration table image example

2.) Next, delete the table related to the migration you just deleted from instruction 1.

Delete table image example

3.) Finally, do the changes you want to the migration file of the table you deleted from instruction no. 2 then run php artisan migrate to migrate the table again.


INSERT INTO homestead.bb_migrations (`migration`, `batch`)  VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2')

something like this


If you can't do what is told by @Martin Bean, then you can try another trick.

Create a new migration and on that file in up() method insert what's in down() method of the migration you want to rollback and in down() method insert what's in up() method.

e.g if your original migration is like this

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}
public function down()
{
    Schema::drop('users');
}

then in new migration file do this

public function up()
{
    Schema::drop('users');
}
public function down()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}

and then run the migrate, it will delete the table. and if you again want that back just rollback it.


Another alternative to those mentioned if you need to do this a bunch of times with the same migration(s). Personally I think this adds a lot of flexibility to your migrations.

Add database/migrations to your autoload object in your composer.json like this:

"autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories",
            "database/support",
            "database/migrations" // add this line
        ]
    },

Then add namespace Database\Migrations; to all of your migration files.

Then run $ composer dump-autoload to refresh your composer.lock file.

Then, assuming your class name for the migration is AlterTableWebDirectories, you can create a command like this:

$ php artisan make:command DropAlterTableWebDirectories

And write this logic in your handle() method:

public function handle {
   (new AlterTableWebDirectories)->down();
   DB::raw("delete from migrations where migration like '%alter_table_web_directories%'");
}

This will do exactly what you want. If you want to decrement the migration count instead of deleting it, you can probably figure out how to change the DB:raw command.

This command could be extended to allow you to dynamically choose which migration you're dropping it by passing an argument into the command.

Then when you're reading to migrate that file again, you can just run php artisan migrate and it will only migrate that one.

This process allows you to make specific changes to migrations without having to do a full refresh and seed each time.

Personally I need to do that a lot because my seeds are rather large.


If you want to rollback last migration.

php artisan migrate:rollback

If you want to rollback specific migration then go to migration table and set highest value of that record in batch. Then.

php artisan migrate:rollback

Currently i'm working on laravel 5.8 if not working any other version of laravel please inform to me.


Use command "php artisan migrate:rollback --step=1" to rollback migration to 1 step back.

For more info check the link :- https://laravel.com/docs/master/migrations#running-migrations


It's quite easy to roll back just a specific migration.
Since the command php artisan migrate:rollback, undo the last database migration, and the order of the migrations execution is stored in the batch field in the migrations table.

You can edit the batch value of the migration that you want to rollback and set it as the higher. Then you can rollback that migration with a simple:

php artisan migrate:rollback

After editing the same migration you can execute it again with a simple

php artisan migrate

NOTICE: if two or more migrations have the same higher value, they will be all rolled back at the same time.


Rollback one step. Natively.

php artisan migrate:rollback --step=1

Rollback two step. Natively.

php artisan migrate:rollback --step=2

It might be a little late to answer this question but here's a very good, clean and efficient way to do it I feel. I'll try to be as thorough as possible.

Before creating your migrations create different directories like so:

    database
       | 
       migrations
            |
            batch_1
            batch_2
            batch_3

Then, when creating your migrations run the following command (using your tables as an example):

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3

The commands above will make the migration file within the given directory path. Then you can simply run the following command to migrate your files via their assigned directories.

    php artisan migrate alter_table_web_directories --path=database/migrations/batch_1

*Note: You can change batch_1 to batch_2 or batch_3 or whatever folder name you're storing the migration files in. As long as it remains within the database/migrations directory or some specified directory.

Next if you need to rollback your specific migrations you can rollback by batch as shown below:

    php artisan migrate:rollback --step=1
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1

or

    php artisan migrate:rollback --step=2
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2

or

    php artisan migrate:rollback --step=3
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3

Using these techniques will allow you more flexibility and control over your database(s) and any modifications made to your schema.


better to used refresh migrate

You may rollback & re-migrate a limited number of migrations by providing the step option to the refresh command. For example, the following command will rollback & re-migrate the last two migrations:

php artisan migrate:refresh --step=2

otherwise used rollback migrate

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last three migrations:

php artisan migrate:rollback --step=3

for more detail about migration see


Laravel 5.3+

Rollback one step. Natively.

php artisan migrate:rollback --step=1

And here's the manual page: docs.


Laravel 5.2 and before

No way to do without some hassle. For details, check Martin Bean's answer.


Every time you rollback you get the last batch of migration. use the command

php artisan migrate:rollback --step=1

If you want modify original migration file and migrate it again, you can use this package to migrate. (Applicable to Laravel 5.4 or later)

First, install package in your Laravel project:

composer require caloskao/migrate-specific

Register command at app/Console/Kernel.php :

protected $commands = [
    \CalosKao\MigrateSpecific::class
];

Now, run this command to migrate your file

php artisan migrate:specific database/migrations/table.php

As stated in the Laravel manual, you may roll back specific number of migrations using the --step option

php artisan migrate:rollback --step=5

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-4

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel Can I do Model->where('id', ARRAY) multiple where conditions? how to fix stream_socket_enable_crypto(): SSL operation failed with code 1 Rollback one specific migration in Laravel How can I resolve "Your requirements could not be resolved to an installable set of packages" error? Define the selected option with the old input in Laravel / Blade Redirect to external URL with return in laravel laravel the requested url was not found on this server

Examples related to laravel-5

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory Can't install laravel installer via composer Including a css file in a blade template? No Application Encryption Key Has Been Specified How to Install Font Awesome in Laravel Mix Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Laravel 5.4 redirection to custom url after login How to set the default value of an attribute on a Laravel model laravel 5.3 new Auth::routes()

Examples related to database-migration

Rollback one specific migration in Laravel Access denied for user 'homestead'@'localhost' (using password: YES) How do I enable EF migrations for multiple contexts to separate databases? EF Migrations: Rollback last applied migration? Reset Entity-Framework Migrations How do I move a redis database from one server to another? How do I copy SQL Azure database to my local development server? How to export SQL Server database to MySQL?