19

I have seeded my DB using php artisan db::seed. Is there a way to rollback what I have seeded into my DB?

I cannot seem to find any command like php artisan db::seed rollback.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Aaron
  • 4,380
  • 19
  • 85
  • 141
  • Did you seed a new (empty) database? Or are you looking to undo some changes and restore the original database? – E Rullmann Jun 23 '17 at 20:54
  • @ERullmann, I am looking to undo the changes I have made using db::seed – Aaron Jun 23 '17 at 20:55
  • That's a lot trickier if you don't have a backup. I don't think that artisan would offer a native service like that. You might want to check to see if there's a log about the SQL commands that were part of seed. Or if all your objects are timestamped with creation times you could delete everything created since you ran the command. – E Rullmann Jun 23 '17 at 21:00
  • @Aaron I know this is old, but revering/rolling back seeds is absolute madness... If you are on production, you have to test everything (before) on another environment... If you are local (dev or similar), backup the DB all the time before testing new features or just test and wipe the DB with `php artisan migrate:fresh`... There is no real need of having a _rollback_ seeder... that means there is a poor implementation or usage of a seeder or similar... The solution is in other place, not in reverting a seeder. – matiaslauriti Aug 27 '21 at 04:29
  • @matiaslauriti Quite contrary I found such ability to be very handy (in development phase) when you need to make quick resets of your DB - I mean resetting ONLY things added through a seed and not anything else and not wiping whole DB. – Alex Aug 27 '21 at 13:53
  • @alex then why aren't you using unit testing ? You would have no need to do this then. I am just saying you are needing this because you are using something wrong or not using something that you should be using at all. – matiaslauriti Aug 27 '21 at 23:05

6 Answers6

18

use Undo Seeder for Laravel.

When you install UndoSeeder, the following artisan commands are made available:

db:seed-undo    Undo seeds in the seeds directory.
db:seed-refresh Undo seeds run seeds again.

more Undo-Seeder

Leo
  • 7,274
  • 5
  • 26
  • 48
16

You may also seed your database using the migrate:refresh command, which will also rollback and re-run all of your migrations. This command is useful for completely re-building your database:

php artisan migrate:refresh --seed

Running Seeders

V-E-Y
  • 169
  • 2
  • 5
  • Is It ther a way run this command but specifying a seeder class? ( like php artisan db:seed --class='CustomSeeder' = – Berni Nov 26 '21 at 15:56
  • @Berni yes, do `Artisan::call('db:seed', ['--class' => 'CustomSeeder']);` and `use Illuminate\Support\Facades\Artisan;` on top. – francisco Jun 07 '22 at 10:27
11

If you want to wipe out certain table, then just TRUNCATE that table, and seed it again:

php artisan db:seed --class=UsersTableSeeder

No need for additional packages for such a simple task.

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
0

The easiest method is to go into your

database/seeders

folder and manually delete the files you want to remove then run php artisan migrate:fresh.

Mayorgeek
  • 21
  • 3
0

Open the database table with whichever platform you are using (phpMyAdmin / mySql-Workbench / a DB-editor plug-in etc.) and manually delete the seeded contents. Then you will be able to reseed the table using php artisan db:seed

Robert Yeomans
  • 192
  • 1
  • 4
-1

I was looking for something else like i have ran php artisan db:seed and after that I wanted to change something in UserSeeder, like changing an email address.

So if you want to change something in Seeder class and you have already run db:seed command.

Then first of all you have to add truncate function before any other code like if you have UserSeeder class then add below code in run function before seeding all User model:

User::truncate();

Then all you have to do is re-run the command.

php artisan db:seed

It will seed again all the classes as per your change and delete already seeded Users in Database, You can use this method for any model you want to truncate the table and re insert the records.

Haritsinh Gohil
  • 5,818
  • 48
  • 50