Java

How to remove unused imports in Intellij IDEA on commit

27 September 2026 · 6 min read

How to remove unused imports in Intellij IDEA on commit

Unused imports are a common nuisance in any codebase. They clutter files, increase cognitive load for developers, and can even lead to subtle build issues or merge conflicts. While IntelliJ IDEA offers powerful features to manage imports, ensuring consistent cleanup across a team, especially before code is committed to version control, remains a challenge for many development teams. Manually scanning and deleting these redundant lines before every commit is tedious and prone to human error, often leading to overlooked imports making their way into the repository. This guide will walk you through the essential configurations and strategies to effectively and automatically remove unused imports in IntelliJ IDEA on commit, significantly enhancing your project’s code quality and streamlining your developer workflow.

The Hidden Costs of Unused Imports and Why Automation is Key

Unused imports might seem harmless, but their cumulative effect can be detrimental to a project’s health. Each redundant import statement adds unnecessary lines to a file, making the code harder to read and navigate. Imagine a large file with dozens of unused imports; it becomes a maze, hindering quick understanding and efficient debugging. This visual clutter can also obscure genuinely important parts of the code, leading to wasted time as developers scan past irrelevant declarations.

Beyond aesthetics, unused imports can sometimes lead to compilation warnings, especially in languages like Java, potentially masking more critical issues. They can also contribute to larger build artifacts, albeit minimally, but more importantly, they can cause merge conflicts during collaborative development if different branches introduce or remove the same unused import. Ensuring a clean codebase is fundamental to maintaining high code quality and reducing technical debt, a principle championed by seasoned developers and organizations like Google, which emphasizes automated tooling for code hygiene. Automation ensures that every piece of code pushed to the repository adheres to a baseline standard, fostering a culture of precision and maintainability.

Relying on developers to manually clean up imports before every commit is an unreliable strategy. It’s an extra step that’s easy to forget in the rush of development, especially when deadlines loom. Automating this process means the burden is lifted from individual developers, guaranteeing consistency and allowing them to focus on feature development rather than mundane cleanup tasks. This proactive approach prevents code pollution at the source, making the collective codebase a more pleasant and robust environment for everyone.

Configuring IntelliJ IDEA for Automatic Import Optimization

IntelliJ IDEA provides robust built-in features to manage and optimize imports automatically. The primary feature for this is “Optimize Imports,” which can be configured to run automatically during various actions, including file saves or code reformatting. By leveraging these IDE settings, you can ensure that your files are clean before you even think about committing them. This proactive cleanup is the first and most crucial step in the process, as it directly addresses the unused imports within your development environment.

To automatically remove unused imports in IntelliJ IDEA on commit, you should configure the IDE to optimize imports on the fly. This setting ensures that whenever you save a file, or before you commit, IntelliJ IDEA will automatically identify and remove any import statements that are not actively used within that file. This significantly reduces manual cleanup effort and maintains a clean codebase without requiring a separate action during the commit process itself.

  1. Access Settings/Preferences: Go to File > Settings (Windows/Linux) or IntelliJ IDEA > Preferences (macOS).
  2. Navigate to Code Style: In the settings dialog, expand Editor and then select Code Style.
  3. Configure Import Settings: Choose your specific language (e.g., Java, Kotlin, Python, TypeScript) from the list. Navigate to the Imports tab.
  4. Enable Wildcard Imports (Optional but Recommended): For Java, you might want to configure when to use star imports (e.g., import java.util.;) versus individual imports. This is more about style than removal, but good to review.
  5. Enable Optimize Imports on the Fly: Go back to Editor > General > Auto Import. For Java, ensure “Optimize imports on the fly” is checked. For other languages, look for similar options within their respective settings, often under “Code Style” or “Inspections.”
  6. Configure Reformat Code Action: Under Editor > Code Style, select your language. Ensure that “Optimize imports” is checked under the “Code cleanup” section when you reformat code (Ctrl+Alt+L or Cmd+Option+L). This is often done before committing.
  7. Enable “Optimize imports” in Commit Dialog: When you go to commit your changes (Ctrl+K or Cmd+K), look for the “Before Commit” section in the commit dialog. Here, you’ll see options like “Reformat code” and “Optimize imports.” Ensure “Optimize imports” is checked. This is the direct mechanism to ensure cleanup specifically on commit.

By following these steps, IntelliJ IDEA will actively assist in keeping your imports tidy. The “Optimize imports on the fly” feature is particularly powerful as it cleans up your code as you write and save, preventing accumulation of unused imports in the first place.

Leveraging Version Control with Pre-Commit Hooks for Enforced Cleanliness

While IntelliJ IDEA’s built-in features are excellent for individual developers, ensuring that everyone on a team consistently cleans up unused imports before committing requires a more robust, enforced solution. This is where version control systems, specifically Git pre-commit hooks, come into play. A Git pre-commit hook is a script that runs automatically before a commit is finalized. If the script exits with a non-zero status, the commit is aborted, forcing the developer to address the issue.

Implementing a pre-commit hook to enforce import optimization is a powerful way to standardize code quality across your entire team. It acts as a gatekeeper, preventing any code with unused imports from ever entering your shared repository. This approach is highly recommended for collaborative projects where maintaining a consistent code style and quality is paramount. Tools like Husky for JavaScript/TypeScript projects or custom shell scripts can be used to set up these hooks effectively.

Here’s why pre-commit hooks are a game-changer for enforcing import cleanup:

  • Guaranteed Consistency: Every commit, regardless of the developer or IDE used, will have its imports optimized.

  • Early Feedback: Developers are immediately notified if their commit contains unused imports, allowing them to fix it before pushing.

  • Reduced Code Review Burden: Code reviewers don’t need to spend time pointing out unused imports; the hook handles it.

  • Automated Enforcement: It removes the reliance on Question & Answer :
    Is there a way to remove unused imports in Intellij IDEA on commit?

    It’s not very optimal to do it manually, CTRL + ALT + O helps, but it’s still manual.

    If you are using IntelliJ IDEA or Android Studio:

    Android optimize imports

    Go to Settings > Editor > General >Auto Import and check the Optimize imports on the fly checkbox.