How to remove unused imports from Eclipse

Eclipse

Eclipse Problem Overview


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

Eclipse Solutions


Solution 1 - Eclipse

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

Solution 2 - Eclipse

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

Solution 3 - Eclipse

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.

Solution 4 - Eclipse

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.

Solution 5 - Eclipse

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

Solution 6 - Eclipse

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

Solution 7 - Eclipse

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 .

Solution 8 - Eclipse

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

Solution 9 - Eclipse

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.

Solution 10 - Eclipse

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

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
QuestionHeisenbugView Question on Stackoverflow
Solution 1 - EclipseHeisenbugView Answer on Stackoverflow
Solution 2 - EclipseAsad NaumanView Answer on Stackoverflow
Solution 3 - EclipseNewUserView Answer on Stackoverflow
Solution 4 - Eclipseuser2589779View Answer on Stackoverflow
Solution 5 - Eclipseniknik123View Answer on Stackoverflow
Solution 6 - EclipseMobileAppDeveloperView Answer on Stackoverflow
Solution 7 - EclipseJesus DimrixView Answer on Stackoverflow
Solution 8 - EclipseRakesh Singh BalharaView Answer on Stackoverflow
Solution 9 - EclipseAndyView Answer on Stackoverflow
Solution 10 - EclipseMikeView Answer on Stackoverflow