How to use IntelliJ IDEA to find all unused code?

165,278

Solution 1

Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group).

Using IntelliJ 11 CE you can now "Analyze | Run Inspection by Name ... | Unused declaration"

Solution 2

In latest IntelliJ versions, you should run it from Analyze->Run Inspection By Name:

enter image description here

Than, pick Unused declaration:

enter image description here

And finally, uncheck the Include test sources:

enter image description here

Solution 3

After you've run the Inspect by Name, select all the locations, and make use of the Apply quick fixes to all the problems drop-down, and use either (or both) of Delete unused parameter(s) and Safe Delete.

Don't forget to hit Do Refactor afterwards.

Then you'll need to run another analysis, as the refactored code will no doubt reveal more unused declarations.

Apply quick fixes to all the problems

Share:
165,278
pathikrit
Author by

pathikrit

Experienced in developing scalable solutions for complex problems. I enjoy working full-stack - from architecting schema and data-flows, implementing algorithms, designing APIs to crafting innovative UIs. My professional interests include algorithms, functional programming, finance, data analytics and visualization.

Updated on October 01, 2020

Comments

  • pathikrit
    pathikrit over 3 years

    When I am in a .java file the unused code is usually grayed out or has a green underline saying this code will probably (probably because of some weird JNI/Reflection corner cases) be unused. But I have this project with thousands of Java files and I want to find ALL INSTANCES of such probable-unused codes. How can I do that in IntelliJ IDEA?

  • ZAD-Man
    ZAD-Man over 10 years
    Just a correction for future readers: IDEA 12.1.6, at least, does indeed have it under Analyze --> Inspect Code
  • Ben Clayton
    Ben Clayton about 10 years
    @CrazyCoder This shows unused fields and methods too, any way to show only unused classes?
  • CrazyCoder
    CrazyCoder about 10 years
    @BenClayton try Unused symbol inspection with appropriate options.
  • Almir Campos
    Almir Campos over 7 years
    My 2 cents: on IntelliJ 2016.3.4 the option is under Analyze > Run Inspection By Name. -- My 5 cents: if you're working on a big project and you're running the feature for the whole project, I suggest you to do it before lunch or at the end of the day, because it can take 1h+ to complete. Or, maybe, you can do it during the work hours and say to your boss that you're running an analyzer... and you can get some rest for free! :)
  • joecizac
    joecizac about 7 years
    @BenClayton and the rest, looking just for unused classes....stackoverflow.com/a/38244028/2541200
  • MrPowerGamerBR
    MrPowerGamerBR over 4 years
    If you are trying to find unused methods in a Kotlin project, run the Unused symbol inspection. Looks like the Unused declaration only works in Java projects.
  • Grzegorz Oledzki
    Grzegorz Oledzki about 2 years
    In newer versions of IntelliJ it's under Code | Analyze Code | Run Inspection by Name