What does bin/rails routes --unused report in Rails 8.1, and how far can you trust it before deleting routes?
answer
- missing controller class
- no action method and no template
- exit status 1 when any found
- static check, not traffic
- missing controller now answers 500
basics
~20 sIt lists routes whose controller class is missing, or whose action has neither a method nor a template, and exits 1 when it finds any. It ignores traffic and links, so a route that passes may still be dead.
solid answer
~50 s`bin/rails routes --unused` (`-u`) boots the app and checks every route statically: a route is **unused** when its controller class cannot be found, or when the controller has no method for the action **and** no template for it. An action rendered implicitly from a template therefore counts as used. It prints "Found N unused routes:" with the usual table, accepts `-c` and `-g`, and exits with status 1 when anything is found, so it can gate CI. The guide describes unused routes as not referenced by any action or view, but the source checks only controller, method and template, never links or traffic. So it finds routes left behind after deleting code; it cannot prove a route with a real action is still needed. That matters more in Rails 8.1, where a route to a missing controller answers 500 instead of 404.
code
bash · 8 lines# list routes with no controller, or no action and no template
bin/rails routes --unused
# narrow it down
bin/rails routes -u -c docks
# in CI: the step fails because the command exits 1 when anything is found
bin/rails routes --unusedgo deeper
Recall that bin/rails routes --unused lists routes whose controller or action no longer exists.
Explain the exact check: missing controller, or missing method and template together, and why implicit rendering counts as used.
Run it in CI, read its limits against the guide's wording, and confirm dead routes with traffic data before deleting them.
Own a routing hygiene policy: static checks in CI, log-based reviews for live routes and a register of URLs that are external contracts.
## What the command checks `bin/rails routes --unused`, short `-u`, hands off to a hidden `unused_routes` command. It boots the app, walks every route and keeps those that look **dead from the code's point of view**. For each route it reads the controller and action names from the route and asks: 1. **Does the controller class exist?** It camelizes the name, appends `Controller` and tries to load it. A missing class makes the route unused. 2. **Does the action exist?** It checks the controller's instance methods for the action name. 3. **Is there a template?** If the method is missing, it looks in the controller's view paths for a template named after the action. A route is reported when the controller is missing, or when **both** the method and the template are missing. An action that has no method but renders `bikes/nearby.html.erb` implicitly counts as used, which matches how Rails would serve it. ```bash $ bin/rails routes --unused Found 2 unused routes: Prefix Verb URI Pattern Controller#Action bike_reports GET /bikes/reports(.:format) bike_reports#index dock_map GET /docks/:id/map(.:format) docks#map ``` It honours the usual filters: `bin/rails routes -u -c docks` or `-u -g reports`. When nothing is found it prints "No unused routes found." ## Using it as a CI gate The command **exits with status 1** when it reports any route, so it can fail a build: ```bash bin/rails routes --unused ``` That catches a common leftover: a controller deleted in a refactor while its `resources` line stays in `config/routes.rb`. ## Why it matters more in Rails 8.1 Before Rails 8.1, a request to a route whose controller did not exist was treated as a routing error and answered **404**. Rails 8.1 treats it as a programming error and answers **500**, so it is reported like any other bug. A dead route left in the table is no longer a quiet 404 for a probing crawler; it is an error in your tracker. Running `--unused` in CI keeps those routes from shipping. ## What it cannot tell you | Question | Can --unused answer it? | |---|---| | Was the controller deleted but the route kept? | Yes | | Was an action method removed with no template left? | Yes | | Does any page link to this route? | No | | Has anyone requested this URL in the last 90 days? | No | | Is this URL a contract with a mobile app or partner? | No | The routing guide describes an unused route as one "not referenced by any controller action or view", which sounds like a link check. The source does no such thing: it checks for a controller, a public method and a template, nothing more. So: - **False reassurance.** A route with a real action that nothing links to, no client calls and no test covers passes the check. Only request logs or analytics show it is dead. - **Generated routes count.** `resources :bikes` with only `index` and `show` actions reports the other five routes, which is the cue to add `only: [:index, :show]` rather than to delete actions. - **External callers.** A webhook or app deep link may be in use even if the route looks odd; check before deleting. ## Reading a report Each reported row points at one of three situations: - **The controller is gone**: delete the route line, or restore the controller if the route is still promised to someone. - **A generated action was never written**: narrow the `resources` call with `only:` or `except:`. - **An action was renamed**: the old route now points nowhere, so update the route to the new action name. ## A safe clean-up routine 1. Run `bin/rails routes --unused` and fix the real leftovers: delete the route lines, or narrow `resources` with `only:`. 2. For routes the tool passes but you suspect are dead, search the request logs for their paths over a meaningful window. 3. Check for external contracts (webhooks, mobile apps, emails already sent with links). 4. Delete the route and its action together, then run the command again. 5. Keep the command in CI so new leftovers fail the build. Used this way, `--unused` is a cheap static lint for routing debris; traffic data decides the rest.
- Why does bin/rails routes --unused report five routes for resources :bikes in a controller with only index and show?`resources` declares seven routes. For `new`, `create`, `edit`, `update` and `destroy` there is no method and no template, so each counts as unused. The fix is usually `resources :bikes, only: [:index, :show]`, which removes the routes instead of adding empty actions.
- In Rails 8.1, what does a request to a route whose controller was deleted return?A 500. Rails 8.1 treats a route pointing at a non-existent controller as a programming error rather than a routing error, so it is reported like any exception instead of answering 404. That makes leftover routes noisier in production and is a good reason to run `bin/rails routes --unused` in CI.
- --unused finds routes that no view or controller links to
- --unused uses request logs to find routes nobody visits
- An action with only a template and no method is reported as unused
- The command always exits 0, so it cannot fail a CI build
- In Rails 8.1 a route to a deleted controller still answers 404
Assumes Rails 8.1: bin/rails routes takes -c, -g, -E and -u, and a route to a missing controller now answers 500 instead of 404.