Safely remove migration In Laravel

PhpDatabaseLaravelArtisan Migrate

Php Problem Overview


In Laravel, there appears to be a command for creating a migration, but not removing.

Create migration command:

php artisan migrate:make create_users_table

If I want to delete the migration, can I just safely delete the corresponding migrations file within the database/migrations folder?

Migrations file:

2013_05_31_220658_create_users_table

Php Solutions


Solution 1 - Php

I accidentally created a migration with a bad name (command: php artisan migrate:make). I did not run (php artisan migrate) the migration, so I decided to remove it. My steps:

  1. Manually delete the migration file under app/database/migrations/my_migration_file_name.php
  2. Reset the composer autoload files: composer dump-autoload
  3. Relax

If you did run the migration (php artisan migrate), you may do this:

a) Run migrate:rollback - it is the right way to undo the last migration (Thnx @Jakobud)

b) If migrate:rollback does not work, do it manually (I remember bugs with migrate:rollback in previous versions):

  1. Manually delete the migration file under app/database/migrations/my_migration_file_name.php
  2. Reset the composer autoload files: composer dump-autoload
  3. Modify your database: Remove the last entry from the migrations table

Solution 2 - Php

If the migration has been run (read: migrated) then you should roll back your migration to clear the history from your database table. Once you're rolled back you should be able to safely delete your migration file and then proceed with migrating again.

Solution 3 - Php

DO NOT run php artisan migrate:fresh that's gonna drop all the tables

Solution 4 - Php

You likely need to delete the entry from the migrations table too.

Solution 5 - Php

DON'T run this on production but this should do the job, if you are in development and the desired outcome is to start all over:

# php artisan migrate:fresh

In production, that maybe not the desired thing, so you should be adverted. (The migrate:fresh command will drop all tables from the database and then execute the migrate command).

Solution 6 - Php

I accidentally created two times create_users_table. It overrided some classes and turned rollback into ErrorException.

What you need to do is find autoload_classmap.php in vendor/composer folder and look for the specific line of code such as

'CreateUsersTable' => $baseDir . '/app/database/migrations/2013_07_04_014051_create_users_table.php',

and edit path. Then your rollback should be fine.

Solution 7 - Php

I agree with the current answers, I just wanna add little more information.

A new feature has been added to Laravel 5.3 and above version that will allow you to back out a single migration:

php artisan migrate:rollback --step=1

after, Manually delete the migration file under database/migrations/my_migration_file_name.php

This is a great feature for when you run a migration

In this way, you can safely remove the migration in laravel only in 2 step

Solution 8 - Php

I will rather do it manually

  1. Delete the model first (if you don't) need the model any longer
  2. Delete the migration from ...database/migrations folder
  3. If you have already migrated i.e if you have already run php artisan migrate, log into your phpmyadmin or SQL(whichever the case is) and in your database, delete the table created by the migration
  4. Still within your database, in the migrations folder, locate the row with that migration file name and delete the row.

Works for me, hope it helps!

Solution 9 - Php

This works for me:

  1. I deleted all tables in my database, mainly the migrations table.
  2. php artisan migrate:refresh

in laravel 5.5.43

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlobalzView Question on Stackoverflow
Solution 1 - PhpmalisokanView Answer on Stackoverflow
Solution 2 - PhpJason LewisView Answer on Stackoverflow
Solution 3 - PhpLastM4NView Answer on Stackoverflow
Solution 4 - PhpStephaneView Answer on Stackoverflow
Solution 5 - PhpjoashView Answer on Stackoverflow
Solution 6 - PhpJR TanView Answer on Stackoverflow
Solution 7 - PhpUdhav SarvaiyaView Answer on Stackoverflow
Solution 8 - PhpDAVID AJAYIView Answer on Stackoverflow
Solution 9 - PhpederrafoView Answer on Stackoverflow