Automatically remove unused imports in IntelliJ IDEA

Organize imports on the fly

The first way is to tell the IDE to automatically organize the imports on the fly. This means that when you are editing a file, if the IDE sees some imports left unused, it automatically removes them, so you don’t have to worry about manually doing that.

In IntelliJ IDEA, you can do this in the File -> Settings… window. Check the “Optimize imports on the fly” checkbox:

intellij-organize-imports

Remove unused imports upon commit with Git

You can also remove the unused imports when committing in Git.

  • Right click on the project where you would like to commit and select Git -> Commit Directory…
  • In the popup that appears check the “Optimize imports” checkbox.

intellij-organize-imports2