How to Remove, Squash, and Clean Up Old Rails Migration Files
As your Rails application grows, so does your db/migrate folder. After years of development, you might have hundreds of migration files that are no longer relevant. If you need a refresher on how migrations work, the Rails Migration Guide covers everything from generating migrations to troubleshooting common errors.
The Problem
Old migrations can cause issues:
- Slow down
rails db:migrateon fresh setups - Confusing for new developers
- May reference models or columns that no longer exist
The Solution
Rails provides a way to squash migrations:
- Ensure your schema is up to date
rails db:migrate
- Verify your schema.rb or structure.sql is correct
Check that db/schema.rb accurately represents your current database.
- Delete old migration files
You can safely delete migrations that have been run on all environments:
rm db/migrate/2020*.rb
rm db/migrate/2021*.rb
- Update schema version
If needed, update the schema version in your schema.rb.
Best Practices
- Only delete migrations older than your oldest production backup
- Make sure all environments have run the migrations
- Consider creating a “squashed” migration for documentation
- After restoring from backups, check for PostgreSQL sequence issues that can cause duplicate key errors
This keeps your codebase clean while maintaining a clear database history.