Undo generate scaffold command in Rails

How to Undo Generate Scaffold Command in Rails

Rails offers the convenient `rails destroy` command to undo the effects of any `rails generate` command. Also, don't forget to rollback the migration, if you have already applied it.

2 min read
TL;DR: Run the bin/rails destroy scaffold Post to undo the changes made by scaffold command in Rails. If you had applied the generated migration, don't forget to rollback the migration using bin/rails db:rollback command first.

The rails generate scaffold command in Rails provides a quick and easy way to generate the major building pieces for your Rails app. It will generate the MVC (model, view, and controller) parts for your domain along with CRUD support. All in a single command.

For example, if you want to generate the basic CRUD functionality for the Post model in your Rails blog, run the following scaffold command.

$ bin/rails generate scaffold Post name:string content:text

Rails will create a bunch of new folders and files in your application, and also edit the config/routes.rb.

What if you made a mistake? Don't worry. Rails provides a simple way to undo the scaffold.

How to Undo Rails Scaffold Command

Simply run the following command:

$ bin/rails destroy scaffold Post

This will delete the files created by the scaffolding process.

If you had applied the migration generated by the scaffold, first rollback it using the db:rollback command, and then destroy the migration.

$ bin/rails db:rollback
$ bin/rails destroy scaffold Post

Also remember that the destroy scaffold command won't undo any other changes you made after generating the scaffold. So keep in mind to undo them all if needed.

In fact, you can use the destroy command to undo any generate command in Rails. For example, if you generated a helper using

$ bin/rails generate helper Payment

You can undo it using this command:

$ bin/rails destroy helper Payment

Hope that helps.


That's a wrap. I hope you found this article helpful and you learned something new.

As always, if you have any questions or feedback, didn't understand something, or found a mistake, please leave a comment below or send me an email. I reply to all emails I get from developers, and I look forward to hearing from you.

If you'd like to receive future articles directly in your email, please subscribe to my blog. If you're already a subscriber, thank you.