LinuxCommandLibrary

rails-destroy

Remove Rails generators' created files

TLDR

List all available generators to destroy

$ rails destroy
copy

Destroy a model named Post
$ rails destroy model [Post]
copy

Destroy a controller named Posts
$ rails destroy controller [Posts]
copy

Destroy a migration that creates Posts
$ rails destroy migration [CreatePosts]
copy

Destroy a scaffold for a model named Post
$ rails destroy scaffold [Post]
copy

SYNOPSIS

bin/rails destroy GENERATOR [args] [options]

PARAMETERS

GENERATOR
    The name of the generator whose actions you wish to reverse (e.g., controller, model, scaffold, migration). This must correspond to a generator previously used.

args
    Arguments specific to the GENERATOR, used to identify the exact files to be destroyed. For instance, for a model, this would be the model name and its attributes (e.g., User name:string).

--force, --no-force
    (Less common for destroy) --force forces overwriting of files, while --no-force prevents overwriting. Typically, destroy deletes, but these options relate to file system interaction.

--skip-namespace
    Skips namespacing the generated files, used to destroy non-namespaced components if they were generated without namespace.

--skip-routes
    Prevents destroy from attempting to remove entries from config/routes.rb. Useful if routes were manually edited or are no longer directly tied to the destroyed component.

--skip-assets
    Skips the destruction of asset files (CSS, JavaScript) that might have been generated alongside a view or controller.

--skip-test
    Skips the destruction of test files associated with the destroyed component. Also accessible via --no-test-framework or --no-test.

DESCRIPTION

The rails destroy command is the inverse operation of rails generate. It is a fundamental command in the Ruby on Rails framework, used to remove files and configurations previously created by Rails generators. When you use rails generate to create models, controllers, scaffolds, or other application components, rails destroy can be used to reverse those actions, cleaning up your project directory and associated configuration files.

This command intelligently targets and deletes files based on the conventions followed by the corresponding generator. For instance, rails destroy model User will attempt to remove the app/models/user.rb file, its associated test files, and the relevant migration file. It also endeavors to undo modifications made to config/routes.rb (e.g., resource declarations) and other configuration files.

It's an invaluable tool for maintaining a clean codebase during development, allowing developers to easily undo experiments or refactorings. While it attempts to revert changes thoroughly, it's always recommended to use version control (like Git) to manage your project, as rails destroy might not catch every custom modification or perfectly clean up complex scenarios.

CAVEATS

rails destroy is a powerful tool, but it's not foolproof. It primarily relies on file naming conventions and the records kept by the generate command.

1. Manual Modifications: If you've manually edited the files created by a generator, or the config/routes.rb file, destroy might not perfectly revert all changes. It will attempt to remove specific lines or files but might leave orphaned code or not touch lines it doesn't recognize.

2. Database Impact: rails destroy model will remove the migration file, but it will not revert the database changes themselves (i.e., it won't drop the table). You need to run bin/rails db:rollback (or db:migrate:down VERSION=...) separately to undo the database schema changes after destroying a migration file.

3. Version Control: Always use rails destroy within a version-controlled project (e.g., Git). This allows you to easily review and revert any unintended deletions or modifications.

4. Incomplete Destruction: In complex scenarios or with heavily customized generators, destroy might not remove all related files or configurations. Manual cleanup may be necessary.

INTERACTIVE CONFIRMATION

rails destroy does not typically ask for confirmation before deleting files. It assumes you know what you're doing. Always be sure of the command before executing it.

TARGETING SPECIFIC GENERATORS

To see what a specific generator would do, you can often run it with a --pretend or --help option, though destroy itself doesn't have a --pretend mode like generate. However, running rails destroy model User and then observing the output before committing to Git (if applicable) is a common practice.

ROUTE CLEANUP

When destroying controllers or scaffolds, rails destroy attempts to remove corresponding entries from config/routes.rb. For instance, destroying a resource like resources :users will often remove that line. If routes are nested or heavily customized, manual adjustment might still be required.

HISTORY

The concept of generate and destroy commands has been integral to the Ruby on Rails framework since its early days, streamlining development by automating repetitive file creation and removal. Initially, some generation tasks were handled by Rake tasks, but with the evolution of the Rails command-line interface (rails executable), generate and destroy became core bin/rails commands. Their functionality has largely remained consistent, adapting to new Rails features (e.g., asset pipeline, Turbolinks, Stimulus) by correctly handling associated file types and configurations. The destroy command specifically was designed as the symmetrical reverse of generate, reflecting Rails' convention-over-configuration philosophy by providing a clean way to undo common development actions.

SEE ALSO

rails generate(1), rails db:migrate(1), rails db:rollback(1), rails routes(1)

Copied to clipboard