How to use IntelliJ IDEA to find all unused code?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
IntelliJ IDEA, developed by JetBrains, is one of the most comprehensive IDEs for software development, particularly popular among Java developers. One of its powerful features is its ability to identify and highlight unused code, which not only helps in cleaning up the codebase but also in enhancing the overall code quality and maintainability.
Understanding Unused Code
Unused code refers to any part of the code that is no longer in use or is not reachable during the runtime, including unused variables, methods, classes, and imports. This can occur for several reasons such as legacy code, refactoring, or changes in requirements. Leaving unused code in the project can lead to confusion, increased maintenance costs, and potential errors.
How to Use IntelliJ IDEA to Find Unused Code
1. Using Code Inspections
IntelliJ IDEA has a built-in feature called inspections which can automatically detect problems ranging from bugs, spelling problems, and also unused code.
Steps to Enable and Use Inspections for Unused Code:
- Open Project Settings: Go to
File>Settings(orIntelliJ IDEA>Preferenceson macOS), then selectEditor>Inspections. - Enable Unused Code Inspections: Look for the
Javasection (or any relevant section for the language you are using), expand it, and check options likeUnused declarationandUnused symbol. Adjust the inspection's severity and options as needed. - Run Inspections: Right-click on any directory in the project explorer that you want to inspect, select
Analyze>Inspect Code. You can choose the whole project or specific scopes. - Review Results: Inspections results will be displayed in a separate window, grouped by categories. Unused code entries will be found under suitable categories.
2. Analyzing Run-time Code Dependencies
IntelliJ IDEA Ultimate also supports detecting unused code based on run-time dependencies:
- Run with Coverage: Start your application with coverage (found under
Run>Run with Coverage). This will monitor which parts of the codebase are actually used during execution. - Analyze Results: After stopping the application, you can browse the coverage results, showing you parts of the code that have never been executed.
3. Usage of Advanced Plugins
For even deeper analysis, consider using additional plugins available through IntelliJ's plugins marketplace. Plugins like SonarLint can provide further insights and highlight dead code that might not be immediately obvious.
Best Practices for Removing Unused Code
- Verify Before Removal: Always ensure that the code is genuinely unused. Sometimes, what seems like unused might be invoked via reflections or used externally.
- Check Under Version Control: Before removing any parts of code, check if it's under version control. This will allow you to retrieve it if needed.
- Refactor Progressively: Prefer gradual cleanup over massive code removals, especially in large projects.
Summary Table: Features and Steps
| Feature | Tool/Method | Steps or Methods Involved | Scope Tips |
| Basic Cleaning | Code Inspections | 1. Enable relevant inspections 2. Inspect Code 3. Review & Act | Use project scope for comprehensive cleanup |
| Runtime Analysis | Run with Coverage | 1. Run application with coverage 2. Analyze non-executed code | Best for dynamic analysis |
| Plugin Support | Third-party Plugins | 1. Install Plugin(s) 2. Run Plugin-specific analysis | Suitable for ongoing code quality monitoring |
By routinely identifying and removing unused code, you can maintain a cleaner, more efficient codebase. IntelliJ IDEA provides robust tools and features to aid in this cleanup, making it an indispensable tool for developers seeking to improve their code quality and maintainability.
Related reading
- How to use Micrometer Timer to record duration of async method returns Mono or Flux
- How to use NSCache
- how to use scipy.optimize.linear_sum_assignment in tensorflow or keras?
- How to use spot instance with amazon elastic beanstalk?
- how to use tf operations in keras models
- How to use the timeit module?
- How to use WeakReference in Java and Android development?
- How to work out the complexity of the game 2048?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.