How to remove unused imports in Intellij IDEA on commit?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Unused imports in your code can lead to unnecessary clutter, increased compile times, and potential confusion, especially in large projects. IntelliJ IDEA provides several features to automate the removal of unused imports, ensuring cleaner and more efficient code. This article will walk you through the steps to remove these imports specifically when you commit your code, using IntelliJ IDEA's robust features.
Technical Explanations
Why Remove Unused Imports?
Unused imports are lines of code that are no longer necessary for the functionality of your program. They may appear due to the refactoring of code, removing methods or shifting functionality, which results in leftover import statements. Removing them:
- Improves Code Readability: Eliminates distractions and makes the code easier to navigate.
- Boosts Performance: Minimizes compilation times since fewer symbols need to be resolved.
- Enhances Maintainability: Reduces the potential for introducing bugs related to unused or outdated classes.
Automation with IntelliJ IDEA
IntelliJ IDEA provides a way to automate this process through code inspections and configurations that can be applied at the time of committing code to a version control system.
Step-by-Step Guide on Removing Unused Imports on Commit
Here's how you can set up IntelliJ IDEA to automatically remove unused imports during commits.
Step 1: Configure Code Cleanup on Commit
- Open Your Project in IntelliJ IDEA: Open the project where you want to automatically remove unused imports.
- Access VCS Settings: Go to
File>Settingsif you are on Windows/Linux orIntelliJ IDEA>Preferenceson macOS. - Navigate to Version Control: Once in the Settings/Preferences dialog, expand
Version Control. - Configure Before Commit Settings:
- Click on
Commit. - Under
Before Commit, you will find multiple options. - Check the option
Perform code analysis. - Ensure that
Optimize importsis checked.
These settings ensure that every time you commit your code, IntelliJ IDEA will optimize imports, removing any that are unused.
Step 2: Manual Optimization as a Backup
In addition to automating the process during commits, it's helpful to know how to manually optimize imports.
- Use the Optimize Imports Feature:
- Open the file you wish to optimize.
- Press
Ctrl + Alt + Oon Windows/Linux orCmd + Option + Oon macOS. - IntelliJ IDEA will then analyze the file and remove any unused imports.
- Batch Optimization Across the Project:
- Go to
Code>Optimize Importswhich will run through the whole project or a selection of files.
Step 3: Code Style Settings
To ensure consistent import management across your entire codebase, configure import rules:
- Access Code Style Settings:
- Navigate to
File>Settingsand expandEditor. - Click on
Code Style>Java.
- Manage Import Rules:
- Within the
Importstab, customize import settings according to your preference, such as grouping and the order of imports.
Table Summarizing Key Steps
| Step | Description |
| Step 1 | Enable Perform code analysis and Optimize imports under Before Commit settings. |
| Step 2 | Press Ctrl + Alt + O (Windows/Linux) or Cmd + Option + O (macOS) for manual optimization. |
| Step 3 | Configure import-related rules under Code Style settings for consistent project formatting. |
Additional Details
Troubleshooting Common Issues
- Performance Concerns: If the process slows down commit times, consider refactoring parts of your codebase that frequently change imports.
- Custom Imports: IntelliJ IDEA might not remove custom annotation imports or those used in JavaDoc. Ensure they are genuinely unused before reporting a missed optimization.
Conclusion
IntelliJ IDEA's features, coupled with your project settings, can significantly enhance your code quality by removing unused imports automatically during commits. By configuring these settings and adhering to best practice import management, you ensure your code remains clean, efficient, and easier to maintain.
By following these guidelines, your development workflow can become smoother, leaving more room for creative coding rather than worrying about code hygiene.
Related reading
- How to reverse a graph in linear time?
- How to rewrite a nested loop using the C STL algorithms?
- How to run TensorFlow on multiple nodes with several CPUs each
- how to save shortest path in dijkstra algorithm
- How to represent empty char in Java Character class
- How to resolve java.lang.NoClassDefFoundError, javax/xml/bind/JAXBException
- How to replace master branch in Git, entirely, from another branch?
- How to reset local git repository?

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.