196

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Leonid
  • 22,360
  • 25
  • 67
  • 91

12 Answers12

164

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.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
117

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 6
    You can also set the option for "Optimize imports on the fly", under the `Editor > Auto Import` section – matt b Sep 25 '12 at 14:41
  • 4
    That is cool, but can drive you crazy if it imports the wrong class. – Peter Lawrey Sep 25 '12 at 14:43
  • 3
    that is what the second option for "Add unambiguous imports on the fly" is for :) You can disable the first and only enable the unambiguous setting. – matt b Sep 25 '12 at 14:44
  • 1
    True, but sometimes you use a class which is fine in one module but isn't in the dependencies of another and you end up including something under `com.sun.*` or `sun.*` – Peter Lawrey Sep 25 '12 at 14:47
  • Not sure if I follow what you mean, but both settings will only add imports for classes on the classpath. You can configure classes or packages to be excluded from automatic import also. – matt b Sep 25 '12 at 14:53
  • This is true, but it much easier to do this when there is multiple options. When there is automatically one choice, its trickier to find how to exclude a package. – Peter Lawrey Sep 25 '12 at 15:02
48

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.

Artur Owczarek
  • 1,146
  • 1
  • 11
  • 22
47

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

Master
  • 2,945
  • 5
  • 34
  • 65
Jay
  • 1,022
  • 10
  • 10
44

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

Roman C
  • 49,761
  • 33
  • 66
  • 176
15

In mac book

IntelliJ

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

8

Or you can do the following shortcut :

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

And write : Optimize Imports

Damiii
  • 1,363
  • 4
  • 25
  • 46
8

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.

Bathri Nathan
  • 1,101
  • 2
  • 13
  • 17
3

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

Steve Pitchers
  • 7,088
  • 5
  • 41
  • 41
2

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

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Shravan Ramamurthy
  • 3,896
  • 5
  • 30
  • 44
1

If you want to use CTRL + Alt + L (remove unused imports while formatting code), you can use CTRL + Alt + Shift + L (once) and there is a checkbox in the popup you should check

enter image description here

1

Now you also have the option to just search for unused imports

  • Code -> Analyze Code -> Run Inspection by Name.
  • Unused Import
  • Whole Project

Source: How to use IntelliJ IDEA to find all unused code?

Josue Abarca
  • 305
  • 3
  • 8