In Rails 8.1, what does bin/rails destroy scaffold Kit undo and leave behind, and what do --pretend, --skip and --force change?
answer
- same generators run with revoke behaviour
- files removed even if you edited them
- the table survives: roll back first
- -p shows the plan, writes nothing
- -s keeps existing files, -f overwrites
basics
~20 sdestroy replays the same generators in reverse: it deletes the files scaffold created and removes the resources :kits line. It does not roll back a migration you already ran, and it deletes files you edited. --pretend previews, --skip keeps existing files, --force overwrites them.
solid answer
~40 s`bin/rails destroy scaffold Kit` (alias `d`) runs the scaffold generator with `revoke` behaviour, so every `create` becomes a `remove`: the migration file, model, tests, fixture, controller, views, helper and JSON views go, and the injected `resources :kits` line is taken back out of `config/routes.rb`. Three things catch people out. Removal ignores your edits, and the views folder is removed whole, so a partial you added under `app/views/kits` goes too. It never touches the database: if `db:migrate` already created `kits`, run `bin/rails db:rollback` first, while the migration file still exists. And lines you added elsewhere, such as `has_many :kits` on `Supplier`, stay. For any generator, `--pretend` (`-p`) prints the actions without writing, `--skip` (`-s`) leaves existing files untouched, and `--force` (`-f`) overwrites them and bypasses the class-name collision check.
code
bash · 4 linesbin/rails generate scaffold Kit name:string --pretend # print the plan only
bin/rails db:rollback # undo the migration first
bin/rails destroy scaffold Kit # then remove the files
bin/rails generate scaffold Kit name:string price:decimal --skip-helper --skip-jbuildergo deeper
Know that destroy removes what a generator created and that --pretend shows what would happen without changing anything.
Explain revoke behaviour, the roll-back-first order, and the difference between --skip and --force when files already exist.
Protect real work: commit before destroying, move custom files out of generated folders, and check reverse associations and schema.rb by hand afterwards.
Decide whether generators are allowed to rewrite code in a mature app at all, and keep regeneration safe through version control and reviewed template changes.
## destroy is generate in reverse Rails generators are built from **Thor actions**: `template`, `create_file`, `empty_directory`, `migration_template`, `route` (an `inject_into_file`). Each action knows how to **invoke** and how to **revoke**. `bin/rails generate` runs a generator with `behavior: :invoke`; `bin/rails destroy` (alias `d`) runs the very same generator, with the same hooks, under `behavior: :revoke`. The status column in the output shows it: each `create` becomes a `remove`. For `bin/rails destroy scaffold Kit`: - the migration `db/migrate/<timestamp>_create_kits.rb` is found by name and removed; - `app/models/kit.rb`, `test/models/kit_test.rb`, `test/fixtures/kits.yml` are removed; - `app/controllers/kits_controller.rb`, `app/helpers/kits_helper.rb` and the controller test are removed; - the `app/views/kits` directory is removed, with the ERB and `.json.jbuilder` views in it; - the `resources :kits` line is taken back out of `config/routes.rb`. ## What destroy does not do | Leftover | Why | What to do | |---|---|---| | The `kits` table and its `schema.rb` entry | destroy only touches files, never the database | `bin/rails db:rollback` **before** destroying, while the migration file exists | | `has_many :kits` you added to `Supplier` | destroy reverts only what the generator wrote | remove it by hand | | Your edits to generated files | Thor's revoke removes the file regardless of content | commit first, so `git` can recover them | | Files you added inside `app/views/kits` | the views folder was created with `empty_directory`, whose revoke is `rm_rf` | move them out first | The leftover table is the classic trap. Destroying first leaves a table with no migration file that created it; `db:rollback` can no longer reverse it because the file holding its steps is gone, and the next `generate model Kit` writes a `create_table :kits` that fails because the table exists. The safe order is roll back, then destroy, then regenerate. ## Previewing and controlling conflicts Every generator, and destroy, accepts Thor's **runtime options**: 1. `--pretend` (`-p`): run the generator and print each action, but write and delete nothing. Use it to check what a scaffold will touch, or what a destroy will remove. 2. `--skip` (`-s`): when a target file already exists, keep it and move on. Useful for regenerating only the missing pieces. 3. `--force` (`-f`): overwrite existing files without asking, and skip the check that refuses a class name already in use. 4. `--quiet` (`-q`): suppress the status output. Without `--skip` or `--force`, Thor stops at each conflicting file and asks (`Overwrite app/models/kit.rb? (enter "h" for help) [Ynaqdhm]`), where `d` shows a diff. Two checks run before anything is written: - **Class collision**: if `Kit` already exists or is reserved, the generator raises `The name 'Kit' is either already used in your application or reserved by Ruby on Rails`, suggesting `--skip-collision-check` or `--force`. - **Migration name collision**: if another migration is already named `create_kits`, the generator raises `Another migration is already named create_kits` and suggests `--force` (or `--skip`). ## The --skip-* family Each sub-generator a generator hooks into is an option, and Thor accepts `--skip-<name>` or `--no-<name>` to turn it off. Commonly used: - `--skip-routes` on `controller` and `scaffold` leaves `config/routes.rb` alone; - `--skip-helper` (or `--no-helper`) skips the helper module; - `--skip-test-framework` skips the tests; - `--no-migration` on `model` writes the class without a migration; - `--no-timestamps` drops `t.timestamps`; - `--skip-jbuilder` skips the JSON views when jbuilder is installed; - `--skip-collision-check` skips the class-name check. The options a given generator accepts are printed by `bin/rails generate scaffold --help`. ## A safe undo checklist 1. `git status` and commit, so anything destroy removes can be recovered. 2. `bin/rails db:migrate:status` to see whether the generated migration has run. 3. If it has, `bin/rails db:rollback` (repeat, or use `STEP=`, if later migrations ran after it). 4. `bin/rails destroy scaffold Kit --pretend` to read the removal list. 5. Move any hand-written files out of `app/views/kits`, then run the real `destroy`. 6. Search for references the generator never wrote: `has_many :kits`, links to `kits_path`, factories or fixtures that mention kits. In a shared branch, prefer a new migration that drops the table over rewriting history; destroy is a tool for the minutes after a typo, not for code teammates have already pulled.
- Why roll back before running bin/rails destroy model Kit rather than after?`db:rollback` reverses the most recent migration by reading its file. Destroy deletes that file, so afterwards Rails has no `down` for the `kits` table, and the table stays in the database and in `schema.rb`. Rolling back first drops the table while the migration still exists; then destroy removes the files cleanly.
- When would you reach for --skip instead of --force on a second generator run?Use `--skip` when you have edited some generated files and only want the missing ones written, for example re-running scaffold after deleting the views. Use `--force` when you deliberately want the generator's fresh output over what is there, typically right after changing the templates in `lib/templates`. Running `--pretend` first shows which files each choice would touch.
- bin/rails destroy scaffold Kit also drops the kits table
- destroy keeps generated files you have edited since
- destroy leaves the resources :kits route in config/routes.rb
- --pretend writes the files but skips the migration
- --skip overwrites files that already exist
- Running db:rollback after destroy still reverses the deleted migration