CodeBriefly
CodeBriefly

Reputation: 1020

Remove specific migration in laravel

As per laravel doc, To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files:

php artisan migrate:rollback

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 five migrations:

php artisan migrate:rollback --step=5

The migrate:reset command will roll back all of your application's migrations:

php artisan migrate:reset

You can check here. But i need to remove the specific migration file. As per my project having 30-40 migration file. I want to remove one of the migration file and its model. Is there any way to do this or have to do it manually.

Upvotes: 5

Views: 12170

Answers (5)

Martin Bean
Martin Bean

Reputation: 39399

Don’t. Migrations are version control for your database. “Removing” a particular migration is like removing a random commit from your Git repository’s history: it can have terrible consequences.

Instead, if you no longer need a table, then create a new migration that drops that table in the up method, and recreates it in the down method so the migration can be rolled back.

Upvotes: 2

Manoj Kumar
Manoj Kumar

Reputation: 848

you can increment the batch number of that particular migration to make it latest batch and run rollback command. Rollback one specific migration in Laravel

Upvotes: 0

DAVID AJAYI
DAVID AJAYI

Reputation: 2154

Just do it manually and save yourself the stress of further issues

  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!

Upvotes: 1

fred
fred

Reputation: 1303

Delete the migration file, remove the table from the database, and also remove that file name from migrations table in the database.

Sometimes, doing things manually is the best way.

Upvotes: 1

CJG
CJG

Reputation: 175

If you simply remove (delete) the migration file and re-run the migrations (migrate:refresh), the database tables will be rebuilt (without the table that's defined in the migration file you deleted).

Upvotes: 0

Related Questions