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?
-
12The 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 '14 at 13:31
-
2@Snicolas right-click on the inspection result (Declaration redundancy->Unused declaration) and choose "Safe delete". – OrangeDog Feb 26 '16 at 11:56
-
Or if you want button, there's the light-bulb in the left toolbar. – OrangeDog Feb 26 '16 at 12:00
-
2You may check my answer on http://stackoverflow.com/questions/22522013/how-find-all-unused-classes-in-intellij-idea/38244028#38244028. There you can find how to filter the results to see only those, which you really wanted. – BlondCode Jul 07 '16 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 '21 at 10:36
4 Answers
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"

- 50,745
- 59
- 165
- 240

- 389,263
- 172
- 990
- 904
-
Just a correction for future readers: IDEA 12.1.6, at least, does indeed have it under Analyze --> Inspect Code – ZAD-Man Jan 14 '14 at 22:27
-
28@CrazyCoder This shows unused fields and methods too, any way to show only unused classes? – Ben Clayton Apr 11 '14 at 13:05
-
@BenClayton try `Unused symbol` inspection with appropriate options. – CrazyCoder Apr 11 '14 at 13:26
-
22My 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! :) – Almir Campos Feb 08 '17 at 03:01
-
2@BenClayton and the rest, looking just for unused classes....http://stackoverflow.com/a/38244028/2541200 – joecizac May 09 '17 at 17:16
-
5If 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. – MrPowerGamerBR Feb 19 '20 at 19:04
-
Do you know if I can adjust search not to include groovy test? I tried configuration proposed, directory marked as test directory but it's still considers code as "used" – Evgenii Aug 16 '23 at 16:05
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:
Than, pick Unused declaration:
And finally, uncheck the Include test sources:

- 14,397
- 15
- 77
- 118
-
7In newer versions of IntelliJ it's under Code | Analyze Code | Run Inspection by Name – Grzegorz Oledzki Mar 28 '22 at 04:51
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.

- 13,235
- 3
- 69
- 45
A picture is worth a thousand words! IntelliJ IDEA 2022.x.x
- Go to the code tab, analyze code, and run inspection by name.
- Select what you need

- 1,374
- 12
- 17