How to remove unused imports in Intellij IDEA on commit?

JavaImportAutomationIntellij Idea

Java Problem Overview


Is there a way to remove unused imports in Intellij IDEA on commit?

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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

When you commit, tick the Optimize imports option on the right. This will become the default until you change it.

I prefer using the Reformat code option as well.

Solution 3 - Java

You can check checkbox in the commit dialog.

enter image description here

You can use settings to automatically optimize imports since 11.1 and above.

enter image description here

Solution 4 - Java

In Mac IntelliJ IDEA, the command is Cmd + Option + O

For some older versions it is apparently Ctrl + Option + O.

(Letter O not Zero 0) on the latest version 2019.x

Solution 5 - Java

Choose the project/module you want to remove unused import from in Project view. Then from Code menu choose Optimize imports and confirm with Run. The imports will be cleaned in whole project/module.

Solution 6 - Java

In mac book

IntelliJ

Control + Option + o (not a zero, letter "o")

Solution 7 - Java

Or you can do the following shortcut :

MAC : Shift + Command + A (Enter Action menu pops up)

And write : Optimize Imports

Solution 8 - Java

To remove the unused imports in windows. please use the default key combination below.

CTRL + ALT + O ---> to remove the unused imports in windows.

However, you can also change the keymap of "Optimize Imports" in settings.

Solution 9 - Java

In IntelliJ, select the project you want to optimize imports on, go to Code menu and choose Optimize imports and a small Optimize Imports popup window will appear. On the popup window you need to click on Run button. Or alternatively, on IntelliJ on Mac, you can use a keyboard short cut Option + Command + O

Solution 10 - Java

File/Settings/Inpsections/Imports and change "Unused import" to Error. This marks them more clearly in the Inspections gutter and the Inspection Results panel.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeonidView Question on Stackoverflow
Solution 1 - JavaMadan SapkotaView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaRoman CView Answer on Stackoverflow
Solution 4 - JavaJayView Answer on Stackoverflow
Solution 5 - JavaArtur OwczarekView Answer on Stackoverflow
Solution 6 - JavaManimaran SamuthirapandiView Answer on Stackoverflow
Solution 7 - JavaDamiiiView Answer on Stackoverflow
Solution 8 - JavaBathri NathanView Answer on Stackoverflow
Solution 9 - JavaShravan RamamurthyView Answer on Stackoverflow
Solution 10 - JavaSteve PitchersView Answer on Stackoverflow