503

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.

3
  • 1
    I am using sqlpro, it let me change the batch number, so i just changed the number and just rollbacked
    – Rolly
    Aug 19, 2016 at 20:09
  • Considering future rollbacks, use 'php artisan migrate --step' to run each migration step by step which will only rollback one migration when you run php artisan migrate:rollback
    – James
    Aug 2, 2019 at 16:32
  • PSA: Please stop adding answers that just copy the top answer, using the rollback with --step. Aug 2, 2023 at 15:19

24 Answers 24

482

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.

5
  • 1
    Not useful unless the OP has been writing his application with the in-development version of Laravel, though. Jun 26, 2016 at 11:53
  • 5
    This would be a good feature, but it's missing the functionality to specify which of the migrations to rollback. I assume it just does the last one listed in the migrations table? When I migrate things, I usually don't plan on rolling back, so the chances of the migration I want to roll back being the last migration are not very high. Seems like laravel kind of dropped the ball on this one. The only safe way to rollback a single migration is to edit the migration table manually I suppose.
    – Skeets
    Sep 7, 2016 at 22:58
  • @SkeetsO'Reilly Agreed, migrations are not extremely well thought. But they are here so you won't be forced to look for migration package right from the start. Sep 8, 2016 at 16:25
  • It worked like a charm. I was developing a new migration (adding a column to an existing table), then I saw I migrated with "uuid" type, I wanted "string". Your rollback tip worked perfectly. Rollback. Update code. Migrate again. => 100% ok. Laravel 5.7 Jan 28, 2020 at 14:21
  • how can i fetch my last N migration steps by (php artisan) command line . if i want, i can seek to db and find my migration table and query to fetch my history and my last migration that run. but in other hand, i cant always use my migration folder because the new migration not always sorted. for this problem i just recommend that we use php artisan make:migration and dont use copy of the migration files from the middle of those existing files. Jun 19, 2020 at 8:55
345

If you look in your migrations table, then you’ll see each migration has a batch number. So when you roll back, it rolls back each migration that was part of the last batch.

If you only want to roll back the very last migration, then just increment the batch number by one. Then next time you run the rollback command, it’ll only roll back that one migration as it’s in a “batch” of its own.

Alternatively, from Laravel 5.3 onwards, you can just run:

php artisan migrate:rollback --step=1

That will rollback the last migration, no matter what its batch number is.

10
  • Good recommendation ! I tried to edit the batch number, it won't let me. It's kind of lock, and grey-out. I tried that in MySQL WorkBench on my Mac. Any idea on that ?
    – code-8
    May 17, 2015 at 14:46
  • Do you know why, my batch number is lock when I tried to configure them ?
    – code-8
    May 18, 2015 at 14:00
  • 2
    Update batch number in migrations table by query like: UPDATE migrations SET batch=2 WHERE migration='name_of_the_migration';
    – Imran Khan
    Oct 16, 2015 at 5:03
  • 4
    @ImranKhan Hard-coding the batch number to ‘2’ isn’t particularly great if you have more than two batches. Apr 2, 2016 at 23:51
  • Use Sequel Pro (for Mac OS only) to easily edit your Migrations table and change batch numbers. Dec 1, 2017 at 19:40
57

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

1
  • 13
    I've used php artisan migrate:rollback --path=/database/migrations/your-specific-migration.php And finally php artisan migrate Oct 22, 2019 at 23:22
32

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

1
  • what does mean rollback in laravel
    – vishal
    Feb 13, 2022 at 12:05
27

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.

5
  • Thanks for your answer, but I'm all set with this. Quick question for you, do you know why I can't edit the batch column of my migration table ?
    – code-8
    May 18, 2015 at 14:01
  • 2
    @ihue little late but the reason you can't edit it using the UI is that the table migrations doesn't have a primary key. You have to write the sql to edit it yourself.
    – Vic
    Sep 8, 2016 at 17:13
  • Very clever, it did what I wanted
    – Gjaa
    Aug 7, 2020 at 22:17
  • I think this is the most good way of doing this. Dec 28, 2020 at 14:00
  • I like this and worth an upvote :) .. I think even better would be to have to migrations. Ome that drops the existing table and one that creates a new one. The first will recreate the table on rollback and the second one will drop the new table. Feb 10, 2021 at 13:46
20

You can use

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

This will remove that specific migration. If the file has foreign key dependency i hoped you use the drop methods. For those cases try creating a new migration and drop foreign if needed.

0
12

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.

Or 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
9

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.

1
  • Unless the laravel documentation is incorrect, "--step=3" means it will rollback the last 3 migrations, not the 3rd to last migration run. I don't know if putting the migrations in different folders would change that at all, but I wouldn't expect it to. If specifying the rollback by name works, that would be great! If you give rollback any arguments, however, it complains "too many arguments".
    – Skeets
    Sep 7, 2016 at 23:17
5

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.

1
  • 1
    was useful for me. Exactly such a case with last migration.
    – CodeToLife
    Apr 29, 2020 at 17:09
4

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.

4

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
1
  • This can be achieved by using php artisan:migrate --path=database/migrations/my_migration.php. Just before You do, make sure the migrations table doesn't have an entry for my_migration.
    – Aleksandar
    Mar 25, 2019 at 11:16
4

To roll back for only one table you should use path option and add migration table path with step one option in command line:

php artisan migrate:rollback --step=1 --path=database/migrations/2022_12_12_050004__alter_table_web_directories.php
4

LARAVEL 10 has a --batch option. See php artisan migrate:rollback --help:

--batch=BATCH - The batch of migrations (identified by their batch number) to be reverted

If you used --step in your php artisan migrate, your migration batch will be unique, so you can specifiy exactly which ONE migration to rollback.

If your php artisan migrate:status is:

2023_04_18_012845_add_security_warnings_metrics .............. [44] Ran
2023_04_20_151238_delete_old_security_warnings_site_metrics .. [45] Ran
2023_04_22_213841_create_a11y_a12y_tables .................... [46] Ran
2023_04_24_013130_remove_checks_queue_high_and_group_size .... [47] Ran

and you want to rollback ONLY batch 46:

php artisan migrate:rollback --batch=46 --pretend
INFO  Rolling back migrations.
2023_04_22_213841_create_a11y_a12y_tables .................. 151ms DONE

And after that, your php artisan migrate:status is:

2023_04_18_012845_add_security_warnings_metrics .............. [44] Ran
2023_04_20_151238_delete_old_security_warnings_site_metrics .. [45] Ran
2023_04_22_213841_create_a11y_a12y_tables ..................... Pending
2023_04_24_013130_remove_checks_queue_high_and_group_size .... [47] Ran

And next migrate --step it'll be batch 48. I always always always do --step, so you can roll back per migration.

3

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.

1
  • In the second image, why did you mention dropping the user table? It makes no sense. Oct 18, 2023 at 3:39
3
php artisan migrate:rollback --path=/database/migrations/0000_00_00_0000_create_something_table.php
0
2

Beyond the aforementioned

php artisan migrate:rollback --step=1

I would add a check to the table migrations:

select * from migrations;

This will show you a ordered list with the migrations that the artisan command is rollingback.

2
php artisan migrate:rollback

If the migration you want to rollback is not in the last batch, you can specify the number of batches to rollback using the --step option example , you can run the following command:

php artisan migrate:rollback --step={{ No. of step }}
php artisan migrate:rollback --step=5
1
  • 1
    The question specifically asks to roll back "one specific migration." This is not an answer to that question.
    – miken32
    Feb 29 at 16:22
1
INSERT INTO homestead.bb_migrations (`migration`, `batch`)  VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2')

something like this

0

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

php artisan migrate:rollback --step=5
2
  • only Laravel 5.3, so anything coded below that version cannot use it
    – Jeffz
    Aug 27, 2016 at 21:29
  • This wouldn't help in the OP's case, since it will rollback a certain number of migrations, not a single specified migration. "--step3" would still rollback all 3 migrations.
    – Skeets
    Sep 7, 2016 at 23:19
0

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.

0

Rolling back a specific migration in Laravel is a straightforward process.

Here are the steps to follow:

  1. First, open your terminal or command prompt and navigate to your Laravel project directory.
  2. Type in the command php artisan migrate:status to view the status of all the migrations in your project.
  3. Identify the migration you want to rollback, and copy its migration name (e.g., 2022_01_01_000000_create_users_table).
  4. Type in the command php artisan migrate:rollback --step=1 --pretend --path=/database/migrations/2022_01_01_000000_create_users_table.php.
  5. Replace the migration name with the name of the migration you want to rollback.
  6. Hit enter, and Laravel will rollback the specified migration.
-1

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.

-1

if you're using phpmy admin, locate the migrations table in your database and edit the batch number of that specific migration:

Rolled back: 2015_05_15_195423_alter_table_web_directories

make sure the batch number of that specific migration is set to the highest number and then run

php artisan migrate:rollback

Only this migration would be rolled back as it is now recognized as the latest migration in the list.

-1

My Answer requires access to Database.

If you have access to database , run following sql query

update migrations set batch = 999999 where migration = '2015_05_15_195423_alter_table_web_directories';

and now run command php artisan migrate:rollback

NOTE: batch = 999999 , here by 999999 we are expecting an integer number that's only assigned for this particular migration and is the greatest in the batch, for example if maximum value of batch in your migration table is 494, you can replace 999999 with 499 as well and then run rollback command.

Other ways are already suggested by other in above answers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.