Heisenbug
Heisenbug

Reputation: 39204

How to remove unused imports from Eclipse

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

Upvotes: 158

Views: 133683

Answers (10)

Rakesh Singh Balhara
Rakesh Singh Balhara

Reputation: 410

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

Upvotes: 4

Heisenbug
Heisenbug

Reputation: 39204

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

Upvotes: 273

Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20986

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

Upvotes: 0

MobileAppDeveloper
MobileAppDeveloper

Reputation: 1049

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

Upvotes: 7

NewUser
NewUser

Reputation: 3819

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.

Upvotes: 51

Jesus Dimrix
Jesus Dimrix

Reputation: 4568

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 .

Upvotes: 3

Asad Nauman
Asad Nauman

Reputation: 969

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

Upvotes: 73

user2589779
user2589779

Reputation: 121

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.

Upvotes: 12

niknik123
niknik123

Reputation: 179

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

Upvotes: 8

Andy
Andy

Reputation: 3692

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.

Upvotes: 0

Related Questions