Beyond db:seed: How to Use db:seed:replant in Rails

Every Rails developer is familiar with rails db:seed, the command used to populate the database with initial data. But what happens when you modify your seeds.rb file and want to start fresh without deleting the entire database? That’s where rails db:seed:replant comes in.

This command is a productive shortcut for anyone who needs to “clean up and start over” with the application’s core data.

The Problem: Duplicate Data and Database Clutter

If you run the rails db:seed command multiple times, Rails simply executes the script again. If your script isn’t smart enough to check if a record already exists (using find_or_create_by, for example), you’ll end up with duplicate data.

The old-school solution was to run rails db:migrate:reset db:seed, but this is slow because it drops the database, recreates the tables, and runs all migrations from scratch.

The Solution: Clear Without Destroying

The rails db:seed:replant command performs two actions atomically:

  1. Truncate: It empties all tables in your database (clearing the data while keeping the table structure intact).
  2. Seed: It executes the db/seeds.rb script.

It is significantly faster than a full reset because it doesn’t touch the schema; it only affects the content.

How to use it:

1
rails db:seed:replant

Why It Matters