264

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
Globalz
  • 4,474
  • 6
  • 34
  • 43

8 Answers8

482

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
cespon
  • 5,630
  • 7
  • 33
  • 47
malisokan
  • 5,362
  • 2
  • 20
  • 19
  • 3
    Thanks. The gottcha for me after deleting a migration was forgetting to run composer dump-autoload – Theo Kouzelis May 27 '14 at 15:31
  • 12
    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. – Jake Wilson Jun 28 '14 at 00:47
  • 4. Delete the actual table from the DB – Jin Jun 19 '17 at 20:24
  • I am wondering how to rollback the changes made a few migration steps ago. Maybe use `migrate:reset`? – Rob Lao Sep 10 '17 at 13:27
  • 2
    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? – djjavo Apr 21 '20 at 10:05
  • is `migrate:rollback` safe in production? will other data in other tables keep intact except the table that just migrated? – guete Mar 24 '21 at 03:51
  • @guete it usually depends. It will be safe as it would only rollback the latest migration operation. You may want to add the `step` parameter. See: https://laravel.com/docs/9.x/migrations#rolling-back-migrations – malisokan Oct 26 '22 at 09:50
69

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.

Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
  • He hasn't run the migration, though. You can't roll back what you didn't actually do, can you? – Stephane Jun 01 '13 at 14:22
  • 11
    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. – Jason Lewis Jun 01 '13 at 14:47
31

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

LastM4N
  • 1,890
  • 1
  • 16
  • 22
14

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

Stephane
  • 1,613
  • 5
  • 20
  • 40
12

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

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
joash
  • 2,205
  • 2
  • 26
  • 31
  • 25
    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. – Goodbytes Nov 05 '18 at 22:18
  • 1
    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! – joash Nov 06 '18 at 16:34
  • 7
    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. – Goodbytes Nov 08 '18 at 14:17
  • This command delete All tables and re-runs migrations rather than remove migration file :| – soroush Feb 16 '22 at 05:44
  • please read before you run this command – joash Feb 16 '22 at 07:50
9

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.

JR Tan
  • 1,703
  • 4
  • 17
  • 35
6

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
6

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!

DAVID AJAYI
  • 1,912
  • 20
  • 13