Naturaily logo with transparent background

How to Remove Old Database Migrations in Ruby on Rails

Rails data migration should be performed in accordance with some rules, tips and notes, so that you won’t need ages to finish it. This is all the more important when you are tasked with creating a clear development environment setup, and removing old records is key. Check how we did that when we overtook one of the Ruby projects.

Goldfish leaping from smaller bowls to larger ones, symbolizing growth and progress.

Recently we’ve overtaken a big project for maintenance and further development. It’s a Rails application with many models, controllers and of course lots of database migrations. There were about one hundred files in db/migrate directory. The question is: how can we setup development environment with current database structure? Running rake db:migrate would just be a suicide. It won’t simply work, because these migrations were written long time ago and the current code and models implementation don’t fit them. Instead let’s load schema.rb file using this command:

tsx

It will create all needed tables for models and also a special table schema_migrations to track migrations. Selecting all rows you will see something like this:

tsx

So, how can we use this special table to get rid of smelly migration files?

The solution

We can just create new migration and copy all add/create statements from schema.rb

tsx

The migration can look like this:

tsx

Then we need to change it a little bit, because we don’t won’t to lose any data while deploying new migration to staging or/and production environment. Just remove force: true option to make sure we will never lose any data by recreating tables. So let’s try to run this new migration:

tsx

Yes, well, it didn’t work, because we’ve already loaded schema.rb and all our tables are already there. So what can we do? We can use the latest version of migration from our schema_migrations and rename the new one:

tsx

Finally, we can remove all migrations excluding the current one:

tsx

Now, if we run db:migrate we won’t see any results or errors because we’ve have already migrated the database. However, if we have to setup a new environment we can run all (one) our migrations without any problems.

What’s left?

There is a small problem with this solution. If you have migrations which are modifying data like adding admin account or so, you need to find them manually and copy the code to our structure migration or add a new migration. The data is not included in schema.rb

Ending…

We just removed all migrations by adding new one with the whole structure. We can now continue hacking and adding new migrations in normal way. Above steps can be repeated any time we want. So, you can keep small number of migrations. Just be careful when you have new migrations on your local branch, but they are not deployed to staging or production yet. It won’t work. You need too rollback your local migrations until it matches staging/production and then copy schema.rb

Thanks all for now. Thank you and bye :)

Let’s Create a Great Website Together

We'll shape your web platform the way you win it!

More posts in this category

  • Core Web Vitals increase in traffic and sales. Graphic presenting man checking the website's metrics.

    September 30, 2025 • 9 min read

    How Much Will Improving Core Web Vitals Actually Increase My Traffic or Sales?

    Are you one of those business owners asking yourself, ‘Why isn’t my site ranking or converting as well as my competitors’? Even after investing in SEO and marketing, something can still feel missing. The problem often goes beyond keywords or ad spend and comes down to how fast and seamless the website feels to users. This is where Core Web Vitals optimization becomes essential.

    READ MORE
  • Illustration of people interacting with a large screen displaying analytics and a magnifying glass highlighting "#1," on a green background. It is a cover photo of a blogpost about Next.js.

    September 24, 2025 • 8 min read

    Next.js Websites for SaaS Scale-Ups: Why They’re the Growth Engine You Need

    SaaS companies in their scale-up phase face a bottleneck that rarely comes from a lack of product vision. The real slowdown lies in their digital infrastructure, most often their marketing and website layer.

    READ MORE
  • Illustration of a person wearing sunglasses and pointing out to a computer monitor. Above the monitor are icons representing visual, hearing, and speech impairments, symbolizing web accessibility and inclusive design.

    September 15, 2025 • 9 min read

    How Does Web Accessibility Impact SEO and Business Growth?

    Think of your website as a store where 1 in 4 customers can’t get through the door. That’s what happens when web accessibility is ignored. Small issues—like missing alt text or tiny buttons—block users, hurt conversions, and increase legal risk. Fixing them is simple, improves usability for everyone, and builds a stronger, more inclusive brand.

    READ MORE