Java Tips - How to get rid of unused import in Eclipse 
Tags: java, eclipse, coding, programming
Published: 2020-10-15 13:57:31

---
title: Java Tips - How to get rid of unused import in Eclipse 
published: true
description: A simple Eclipse keyboard shortcut to get rid of unused imports in Eclipse IDE
tags: Java, Eclipse, Coding, Programming 
cover_image: https://miro.medium.com/max/513/0*aa-RIR0uF20kDxEl.PNG
---
*Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.*

Hello guys, if you are a Java programmer and use Eclipse IDE for Java development then you might have noticed a lot of unused imports in your Java file while working on a legacy project. [Eclipse IDE](http://www.java67.com/2018/02/5-free-eclipse-and-junit-online-courses-java-developers.html) gives the warning** "The import XXX is never used"** whenever it detects an *unused import* in a Java source file and shows a yellow underline.

Though the unused import in a Java file **does not create any harm**, it's unnecessary to increase the length and size of a Java source file, and if you have too many unused imports in your Java source file, those yellow underlines and Eclipse warnings affect the readability of your code, at least it distracts you.

In my last post on Eclipse, we looked at some [Java debugging tips on Eclipse](https://javarevisited.blogspot.com/2011/07/java-debugging-tutorial-example-tips.html). 

In this post, you will learn some useful Eclipse shortcut to remove all unused imports from Java code in Eclipse.

There are many options to tackle this problem like you can collapse the import section of code in Eclipse or you can altogether remove all unused imports from the Java file, and we'll see them in this short tutorial.

If you are completely new to Eclipse IDE, then I suggest you to first go through a comprehensive course, like [**The Eclipse Guided Tour: Part 1 and 2**](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Feclipse-guided-tour-part1) from [Pluralsight](https://javarevisited.blogspot.com/2017/12/top-10-pluralsight-courses-java-and-web-developers.html), which will teach you everything you need to know about Eclipse from the Java development point of view. 


[![best Eclipse course for Java developers](https://dev-to-uploads.s3.amazonaws.com/i/5wgdkufh0auy4r93fx0z.png)](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Feclipse-guided-tour-part1)

Anyway, let's see *how to remove all unused imports from a Java file in Eclipse* IDE.

## How to Remove all Unused Imports From a Java File in Eclipse

![How to Remove all Unused Imports From a Java File in Eclipse](https://miro.medium.com/max/513/0*aa-RIR0uF20kDxEl.PNG)

*The yellow underline on the above pic denotes all unused imports in a Java Source file*

Here are **a couple of ways to remove all unused imports** from the [Java Eclipse IDE](https://javarevisited.blogspot.com/2020/05/top-5-courses-to-learn-eclipse-ide-for-java-developers.html) :

1) Go to the line of unused import, press Ctrl + 1, which is an Eclipse shortcut of a quick fix. This will show a drop-down menu to fix this error and one of them will be "remove unused imports." It will remove that import statement from the Java file.

2) The above option is not very efficient if you want to remove multiple unused imports in a single click because it removes them one by one. It's better to use the **"Organize Imports**" feature of Eclipse IDE to remove multiple or all unused imports statement.

For using this form Menu, Select Source → Organize Imports. This will remove all unused imports from that Java file.

3) Third and my preferred option to remove all unused import statements from Java file is Ctrl + Shift + O, which is a **shortcut of organized import** in Eclipse, and you can say *Eclipse shortcut to remove all unused import statement* from the Java file.

This will also work with the previous option and remove all unused import lines from the source file.

[ ![Eclipse shortcuts to remove unused importst](https://miro.medium.com/max/533/0*o-GRXFz9nnXa6qXW.png)](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Feclipse-debugging-techniques-and-tricks%2F)

### Bonus Tip
Do you want a **bonus Eclipse tip?** If yes, then here you go: you can save all keystrokes required to remove all unused imports from Java source file by using **Eclipse save actions**, which are actions Eclipse automatically performs when you save Java file.

Just check the option of organized imports in saving action under Windows → Preferences-> Java → Editor → Save Actions. This saves a lot of time while working in Eclipse and is one of the reasons I love Eclipse IDE for Java programming.

You can also configure formatting and a few other actions, which will be taken by Eclipse automatically whenever you save your file. If you want to learn more about such features, I also suggest you take a look at [**Beginners Eclipse Java IDE Training Course**](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fbeginners-eclipse-java-ide-training-course%2F)on Udemy. One of the better course to learn Eclipse IDE.


One more advantage of using **Organize import** or the shortcut **Ctrl + Shift +O** is that it imports all packages that are required by code.

So, it not only saves time while removing the import statement but it also helps to import required packages in Java.

It means that if you copy and paste the code and see lots of red lines, this is an error due to the non-imported packages. To fix this, use this Eclipse shortcut or save your source file if you have configured *Eclipse save action* to organize import.

That's all on **Eclipse shortcuts for removing all unused import statements from a Java source file in Eclipse IDE**. It's always good to learn more about the IDE on which you are working like [IntelliJIDEA](https://itnext.io/top-5-intellijidea-and-android-studio-courses-for-java-and-android-programmers-afcc27309b60), Netbeans, or Eclipse, to improve productivity.

####Further Learning
1.  [Beginners Eclipse Java IDE Training Course](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fbeginners-eclipse-java-ide-training-course%2F)
2.  [Eclipse Debugging Techniques and Tricks](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Feclipse-debugging-techniques-and-tricks%2F)
3.  [The Eclipse Guided Tour: Part 1 and 2](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Feclipse-guided-tour-part1)



Other **Java Eclipse articles** you may like to explore:

-   [30 Useful Eclipse Shortcuts for Java Developers] (http://javarevisited.blogspot.com/2010/10/eclipse-tutorial-most-useful-eclipse.html))
-   [How to remote debug Java application in Eclipse?](http://javarevisited.blogspot.sg/2011/02/how-to-setup-remote-debugging-in.html))
-   [10 Eclipse Debugging Tips Java Developer Should Know?](http://javarevisited.blogspot.com/2011/07/java-debugging-tutorial-example-tips.html))
-   [How to Attach Source Code for the JAR File in Eclipse?](http://javarevisited.blogspot.com/2012/12/how-to-attach-source-in-eclipse-Jar-JDK-debugging.html))
-   [Eclipse Shortcut to Print System.out.println Statements?](http://javarevisited.blogspot.com/2012/10/eclipse-shortcut-to-systemoutprintln-in-java.html))
-   [How to increase the console buffer size in Eclipse?](http://javarevisited.blogspot.com/2013/03/how-to-increase-console-buffer-size-in.html))
-   [How to use spaces instead of tabs in Eclipse?](http://javarevisited.blogspot.com/2016/06/how-to-make-eclipse-use-spaces-instead.html))
-   [How to create an executable JAR file from Eclipse?](http://java67.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.html))
-   [3 Books to Learn Eclipse IDE for Java developers](http://javarevisited.blogspot.com/2016/03/3-books-to-learn-eclipse-ide-for-java-jee-programmers.html))
-   [How to Increase Heap Size of Java Program running in Eclipse?](http://www.java67.com/2016/02/how-to-increase-heap-size-of-java-program-eclipse-command-line.html))

Since Eclipse is one of the popular Java IDE and many Java programmer, who like to do things fast loves to learn new shortcuts. If you also like to learn new shortcuts, then I suggest you check [**Top 30 Eclipse keyboard shortcut for Java programmer**](https://javarevisited.blogspot.com/2018/07/top-30-eclipse-keyboard-shortcuts-java-programming.html). This is one of the many Eclipse shortcuts I have shared there.

Once again, thanks for reading this article!. If you like this post, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a comment below.