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 !– SnicolasAug 14, 2014 at 13:31
-
2@Snicolas right-click on the inspection result (Declaration redundancy->Unused declaration) and choose "Safe delete".– OrangeDogFeb 26, 2016 at 11:56
-
Or if you want button, there's the light-bulb in the left toolbar.– OrangeDogFeb 26, 2016 at 12:00
-
2You 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.– BlondCodeJul 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.– ersegunOct 30, 2021 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"
-
Just a correction for future readers: IDEA 12.1.6, at least, does indeed have it under Analyze --> Inspect Code– ZAD-ManJan 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
-
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! :) Feb 8, 2017 at 3:01
-
3@BenClayton and the rest, looking just for unused classes....stackoverflow.com/a/38244028/2541200– joecizacMay 9, 2017 at 17:16
-
5If you are trying to find unused methods in a Kotlin project, run the
Unused symbol
inspection. Looks like theUnused declaration
only works in Java projects. Feb 19, 2020 at 19:04
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:
-
7In newer versions of IntelliJ it's under Code | Analyze Code | Run Inspection by Name Mar 28, 2022 at 4: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.
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 🚀