409

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 code. How can I do that in IntelliJ IDEA?

5
  • 12
    The answer given is right, but it's just a pity that there is no way to get all unused /dead code inside a project + a button to remove it all. That would be helpful !
    – Snicolas
    Aug 14, 2014 at 13:31
  • 2
    @Snicolas right-click on the inspection result (Declaration redundancy->Unused declaration) and choose "Safe delete".
    – OrangeDog
    Feb 26, 2016 at 11:56
  • Or if you want button, there's the light-bulb in the left toolbar.
    – OrangeDog
    Feb 26, 2016 at 12:00
  • 2
    You may check my answer on stackoverflow.com/questions/22522013/…. There you can find how to filter the results to see only those, which you really wanted.
    – BlondCode
    Jul 7, 2016 at 11:24
  • Is there a way of automating IntelliJ's unused code detection mechanism in a pull request ? When a pull request is created, so it will scan through the files in that PR, and show me the unused code.
    – ersegun
    Oct 30, 2021 at 10:36

4 Answers 4

591

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"

7
  • Just a correction for future readers: IDEA 12.1.6, at least, does indeed have it under Analyze --> Inspect Code
    – ZAD-Man
    Jan 14, 2014 at 22:27
  • 29
    @CrazyCoder This shows unused fields and methods too, any way to show only unused classes? Apr 11, 2014 at 13:05
  • 22
    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! :) Feb 8, 2017 at 3:01
  • 3
    @BenClayton and the rest, looking just for unused classes....stackoverflow.com/a/38244028/2541200
    – joecizac
    May 9, 2017 at 17:16
  • 5
    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. Feb 19, 2020 at 19:04
126

In newer versions of IntelliJ it's under Code -> Analyze Code -> Run Inspection by Name.

In IntelliJ versions before that, 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

1
  • 7
    In newer versions of IntelliJ it's under Code | Analyze Code | Run Inspection by Name Mar 28, 2022 at 4:51
4

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

4

A picture is worth a thousand words! IntelliJ IDEA 2022.x.x

  • Go to the code tab, analyze code, and run inspection by name.

navigation

  • Select what you need 🚀

settings

1
  • As of today (WebStorm Giraffe) there is no Inspection Options section
    – Hung Vu
    Sep 19 at 8:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.