How to remove unused imports from Eclipse
Asked Answered
H

10

158

Is there any way to automatically remove all unused imports (signaled with a warning) of a project with Eclipse IDE?

Humphreys answered 16/4, 2011 at 13:33 Comment(6)
@Dibya: Please do not randomly add language tags to questions that are not about a language. Thank you.Boresome
The answers seem to mention Java; I was looking for this for C++. Does this work the same on a C++ project in Eclipse?Juline
@dwanderson: sorry I used Eclipse mostly with Java. I don't think this works with C++.Humphreys
@Heisenbug: Would you consider adding Java tag?Rancell
@NewUser: if it's Java only (probably it is), yes I (or you) can edit and fix the missing tag!Humphreys
@Heisenbug: I did add the Java tag :) Please do not randomly add language tags to questions that are not about a language. was intended to me! :)Rancell
H
270

I just found the way. Right click on the desired package then Source -> Organize Imports.

Shortcut keys:

  • Windows: Ctrl + Shift + O
  • Mac: Cmd + Shift + O
Humphreys answered 16/4, 2011 at 13:38 Comment(4)
it's also quite helpful do define a save-action for this. Preferences->Java->Editor->Save ActionsIgnaciaignacio
Note that this will also 'clean' them up by moving them around to a more presentable manner.Candide
How about Android Studio?Natoshanatron
funny enough i had the situation where the import list contains an public static inner class from the same file and the 'organize imports' could not remove the unnecessary entry. As the class wasn't used i made it private and the organizer was successfull...Barter
D
73

You can direct use the shortcut by pressing Ctrl+Shift+O

Davinadavine answered 18/2, 2013 at 13:38 Comment(1)
@minil if you mark the project/package in the Package explorer and then do Ctrl+Shift+O the whole project/package wil be cleaned of unused importsDemisec
R
49

I know this is a very old thread. I found this way very helpful for me:

  1. Go to Window → Preferences → Java → Editor → Save Actions.
  2. Check the option "Perform the selected actions on save".
  3. Check the option "Organize imports".

Now every time you save your classes, eclipse will take care of removing the unused imports.

Rancell answered 11/12, 2013 at 7:10 Comment(0)
B
12

Remove all unused import in eclipse:

Right click on the desired package then Source->Organize Imports. Or You can direct use the shortcut by pressing Ctrl+Shift+O

Work perfectly.

Benelux answered 17/7, 2013 at 3:41 Comment(0)
D
8

If you want to do this on whole project then inside project -->select src folder-->press Ctrl+Shift+O

Downes answered 26/3, 2013 at 6:24 Comment(0)
S
7

Use ALT + CTRL + O. It will organize all the imports. You can find various other options in the "Code" Menu.

EDIT: Sorry it is CTRL + SHIFT + O

Stocky answered 25/3, 2014 at 17:34 Comment(1)
I think it will be cntrl + shift + ODiplomacy
B
4

press Ctrl+Shift+O and it will remove unwanted imports

Beckett answered 22/1, 2018 at 8:53 Comment(2)
This seems to be just a repeat of many of the existing answers.Navigation
Good, Straight forward Answer to comment out unnecessary import statements in EclipseImmoderacy
D
3

Better way is just to add "save action" so when you save the project it will clear the unused import's and format the code as well if you like .

Go to Window > Preferences > Java > Editor > Save Actions

and pick what ever you want .

Droll answered 24/4, 2014 at 8:51 Comment(0)
E
0

Certainly in Eclipse indigo, a yellow line appears under unused imports. If you hover over that, there will be multiple links; one of which will say "Remove unused import". Click that.

If you have multiple unused imports, just hover over one and there will be a link that allows you to remove all unused imports at once. I can't remember the exact wording off hand, but all the links that appear are pretty self explanatory.

Equipollent answered 3/9, 2011 at 15:38 Comment(1)
This applies to a single file only, not to a whole project.Gospel
C
0

Not to reorganize imports (not to unfold .* and not to reorder lines) to have least VCS changeset
you can use custom eclipse clenup as this answer suggests

Code answered 6/4, 2016 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.