Safely remove migration In Laravel
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:
- Manually delete the migration file under
app/database/migrations/my_migration_file_name.php
- Reset the composer autoload files:
composer dump-autoload
- 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):
- Manually delete the migration file under
app/database/migrations/my_migration_file_name.php
- Reset the composer autoload files:
composer dump-autoload
- 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).
Related videos on Youtube
Globalz
Updated on July 17, 2022Comments
-
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 almost 11 yearsHe hasn't run the migration, though. You can't roll back what you didn't actually do, can you?
-
Jason Lewis almost 11 yearsNo 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 about 10 yearsIf you - like I did - just went along and renamed a migration file this is the answer you are looking for! Thanks.
-
Theo Kouzelis almost 10 yearsThanks. The gottcha for me after deleting a migration was forgetting to run composer dump-autoload
-
Jake Wilson almost 10 yearsIf 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 over 9 yearsYou could also just simply do "composer dumpautoload"
-
Imran Khan over 8 yearsgreat answer for steps after (php artisan migrate) (Y)
-
bypabloc_ over 7 yearsMy God and I tormented myself when I was wrong. Thank you.
-
Jin almost 7 years4. Delete the actual table from the DB
-
Rob L over 6 yearsI am wondering how to rollback the changes made a few migration steps ago. Maybe use
migrate:reset
? -
Izac Mac over 6 yearsAbsolute lifesaver.
-
javier_domenech over 6 yearsI like step 3 .
-
Goodbytes over 5 years3 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 over 5 yearskindly 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 over 5 yearsmigrate: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 about 4 yearsGreat 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 over 3 yearsMy puppy and me are grateful for your answer, it works!
-
guete about 3 yearsis
migrate:rollback
safe in production? will other data in other tables keep intact except the table that just migrated? -
Wael Alshabani almost 3 yearsI love how this is not an answer to the question, but yet useful and save lives.
-
soroush about 2 yearsThis command delete All tables and re-runs migrations rather than remove migration file :|
-
joash about 2 yearsplease read before you run this command
-
alex toader about 2 yearsDelete migration and the row in the migrations table. This looks like the most straightforward solution to me.