How to 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.
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
This keeps your codebase clean while maintaining a clear database history.