How to remove unused imports in Intellij IDEA on commit?
Asked Answered
T

12

202

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.

Touchmenot answered 25/9, 2012 at 14:34 Comment(5)
ctrl + alt + l will send my pc to sleep..LOL. linux btw.Barragan
alt + control + o : works best on MacJylland
@Barragan you can disable it through All Settings => Keyboards => Shortcuts . By the way <kbd>ctrl</kbd> <kbd>alt</kbd> <kbd>l</kbd> is used to format the code in IntellijJuline
alt + control + o working in windows + IDEAAgler
Mac: option + control + oWept
P
118

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.

Portiere answered 25/9, 2012 at 14:37 Comment(6)
You can also set the option for "Optimize imports on the fly", under the Editor > Auto Import sectionPansypant
That is cool, but can drive you crazy if it imports the wrong class.Portiere
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.Pansypant
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.*Portiere
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.Pansypant
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.Portiere
M
170

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.

Molality answered 4/6, 2015 at 3:25 Comment(2)
I just tried and it does not remove unused imports (on IDEA 2017.2.4)Mons
For me its Preferences > Editor > General > Auto Import, and check the "Optimize imports on the fly". It works. I'm using IntelliJ IDEA 2020.1.4Terpstra
P
118

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.

Portiere answered 25/9, 2012 at 14:37 Comment(6)
You can also set the option for "Optimize imports on the fly", under the Editor > Auto Import sectionPansypant
That is cool, but can drive you crazy if it imports the wrong class.Portiere
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.Pansypant
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.*Portiere
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.Pansypant
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.Portiere
E
50

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

Erigeron answered 8/8, 2017 at 20:50 Comment(2)
and ctrl+alt+o for WindowsSwingle
Update for 2020. We use ctrl + option + o (macOS)Anabasis
A
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.

Alburga answered 22/2, 2016 at 10:52 Comment(0)
F
45

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

Faeroese answered 25/9, 2012 at 14:40 Comment(0)
O
15

In mac book

IntelliJ

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

Overlay answered 12/8, 2019 at 13:47 Comment(0)
F
9

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.

Friesland answered 29/6, 2021 at 11:8 Comment(0)
F
8

Or you can do the following shortcut :

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

And write : Optimize Imports

Forgiving answered 3/12, 2018 at 17:2 Comment(0)
B
5

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

Burley answered 27/1, 2023 at 17:30 Comment(0)
I
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.

Inbreathe answered 12/9, 2019 at 10:27 Comment(0)
G
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

Golightly answered 18/9, 2017 at 22:20 Comment(1)
Its Option+ Control +OIsostasy
V
2

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?

Velvety answered 29/3, 2023 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.