Skip to content

EF Core Remove Migration — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about EF Core Remove Migration. We cover key concepts, practical examples, and best practices.

You add a migration but realize the model change is wrong, incomplete, or unnecessary. The migration is not yet applied to the database. You want to remove it and regenerate. dotnet ef migrations remove deletes the last unapplied migration.

Wrong

// Manually delete the migration files
// But forget to update the ModelSnapshot — now snapshot is out of sync

Output: Next migration is incorrect because ModelSnapshot.cs still reflects the old migration. Schema drift and errors.

# Remove the last migration (not applied to database)
dotnet ef migrations remove

Output: The migration .cs file and the .Designer.cs file are deleted. The ModelSnapshot.cs is reverted to the previous state.

If the migration was already applied to the database:

# First roll back the database
dotnet ef database update PreviousMigration
# Then remove the migration
dotnet ef migrations remove

Force remove (even if applied — use with caution):

dotnet ef migrations remove --force

Prevention

  • Use dotnet ef migrations remove instead of manually deleting migration files.
  • Run dotnet ef migrations list to see which migrations are applied vs pending.
  • Only remove the latest migration — remove older migrations by reverting to them.
  • Remove a bad migration immediately before anyone else applies it.
  • Check ModelSnapshot.cs after removal to confirm it reverted correctly.
  • Use --force only when you know the database has been rolled back.
  • Always review the diff after removing a migration.

Common Mistakes with core migration remove

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world EF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can I remove a migration that is not the latest?

No. dotnet ef migrations remove only removes the last migration. To remove an older migration, you must first remove all newer migrations, then remove the target one. Then re-add the newer ones (if still needed).

What happens if I manually delete migration files?

The ModelSnapshot.cs becomes out of sync — it still reflects the deleted migration. Subsequent migrations add commands produce incorrect migrations. Always use the CLI tool to remove migrations cleanly.

How do I undo a migration that was applied to production?

Create a new migration that reverts the changes: migrationBuilder.DropColumn(...) or migrationBuilder.AlterColumn(...). Name it something like Revert_BadMigration. Apply it normally. Never use migrations remove on applied production migrations.

Learn more about EF Core migrations at DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro