Safely remove migration In Laravel

211,083

Solution 1

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

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

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

Solution 4

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

Solution 5

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).

Share:
211,083

Related videos on Youtube

Globalz
Author by

Globalz

Updated on July 17, 2022

Comments

  • Globalz
    Globalz almost 2 years

    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
    
  • Stephane
    Stephane almost 11 years
    He hasn't run the migration, though. You can't roll back what you didn't actually do, can you?
  • Jason Lewis
    Jason Lewis almost 11 years
    No you can't, but if that's the case then there should be no history stored in the migrations database which means you can safely delete the file.
  • Berdus
    Berdus about 10 years
    If you - like I did - just went along and renamed a migration file this is the answer you are looking for! Thanks.
  • Theo Kouzelis
    Theo Kouzelis almost 10 years
    Thanks. The gottcha for me after deleting a migration was forgetting to run composer dump-autoload
  • Jake Wilson
    Jake Wilson almost 10 years
    If you have run the migration, the "proper" way is to run migrate:rollback to rollback the migration, then delete the migration file and dump autoload. No need to hack the db or the migrations table.
  • FooBar
    FooBar over 9 years
    You could also just simply do "composer dumpautoload"
  • Imran Khan
    Imran Khan over 8 years
    great answer for steps after (php artisan migrate) (Y)
  • bypabloc_
    bypabloc_ over 7 years
    My God and I tormented myself when I was wrong. Thank you.
  • Jin
    Jin almost 7 years
    4. Delete the actual table from the DB
  • Rob L
    Rob L over 6 years
    I am wondering how to rollback the changes made a few migration steps ago. Maybe use migrate:reset?
  • Izac Mac
    Izac Mac over 6 years
    Absolute lifesaver.
  • javier_domenech
    javier_domenech over 6 years
    I like step 3 .
  • Goodbytes
    Goodbytes over 5 years
    3 upvotes? the OP asked for a way to delete a migration, not destroy and refresh the entire database. This is awful advice, don't do this unless you know what you're doing.
  • joash
    joash over 5 years
    kindly read about the difference between migrate:refresh and migrate:fresh you seen to be describing the first, the later in way does partial reset avoiding the manual work!
  • Goodbytes
    Goodbytes over 5 years
    migrate:fresh immediately drops ALL tables and re-runs migrations as if running for the first time. There is nothing partial about it.. any data will be gone. It will fix the problem but it's not a valid answer to the question.
  • djjavo
    djjavo about 4 years
    Great answer! I know that your answer is for an older version but I believe that running composer dump-autoload is no longer needed from Laravel 6.x. Might be worth noting for future reference?
  • Fernando Torres
    Fernando Torres over 3 years
    My puppy and me are grateful for your answer, it works!
  • guete
    guete about 3 years
    is migrate:rollback safe in production? will other data in other tables keep intact except the table that just migrated?
  • Wael Alshabani
    Wael Alshabani almost 3 years
    I love how this is not an answer to the question, but yet useful and save lives.
  • soroush
    soroush about 2 years
    This command delete All tables and re-runs migrations rather than remove migration file :|
  • joash
    joash about 2 years
    please read before you run this command
  • alex toader
    alex toader about 2 years
    Delete migration and the row in the migrations table. This looks like the most straightforward solution to me.