How to remove unused code with IntelliJ IDEA

Viktor Reinok
Apr 1, 2024

At some point, a project could accumulate dead code that no longer serves its purpose and is cluttering the production code. Dead code is confusing and is a great addition to the technical dept.

Code | Analyze code| Run inspection by name

Dialogue window in IDEA

The problem

One problem is that IDEA will ignore that unused source code is referenced in tests. The solution would be to temporarily remove all the test sources by adding all temporarily removed test files to a separate Changelist under Local Changes in the menu so that relevant changes will not be cluttered.

Things to be aware of

  • Do not remove provides:
    - Rest endpoints
    - Scheduled tasks
  • Reflection or access by instrumentation

References

https://stackoverflow.com/questions/6587729/how-to-use-intellij-idea-to-find-all-unused-code

--

--