350
votes

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?

3
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
@Snicolas right-click on the inspection result (Declaration redundancy->Unused declaration) and choose "Safe delete".OrangeDog
Or if you want button, there's the light-bulb in the left toolbar.OrangeDog
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

3 Answers

532
votes

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"

84
votes

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

2
votes

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