

Then from Code menu choose Optimize imports and confirm with Run. Choose the project/module you want to remove unused import from in Project view.

You can use settings to automatically optimize imports since 11.1 and above. How do I delete unnecessary imports in IntelliJ? In the Settings/Preferences dialog Ctrl+Alt+S, click Editor | General | Auto Import. IntelliJ IDEA will remove or modify import statements according to the rules specified in Settings/Preferences | Editor | Code Style | | Imports as you work in the editor. However, you can change the location for storing those files, if necessary. Which is why I think IDEA just doesn’t provide this behavior: it might give users false expectations of safety, and removing them is not a safe refactoring.You might be interested: Are Cashews Gluten? Where does IntelliJ store settings?īy default, IntelliJ IDEA stores user-specific files for each IDE instance (configuration, caches, plugins, logs, and so on) in the user’s home directory. So the IDE can’t guarantee that removing those classes won’t change externally observable behavior. So even if they’re not apparently used within the code of your project, they might be used depending on what you or external code using your library runs. The compiler can determine this by looking at just your code.įor classes, even if they don’t have public access, they can be referenced dynamically with Class.forName(), and this actually happens in live code.

(Well, at least without doing things with Reflection or JVM hacking that you’re not supposed to.) No matter what outside code does with your library, it won’t cause those things to be used, because their scope is limited and the IDE can see all of it. The unused declarations IDEA (and AFAIK, NetBeans) looks for are for private members and local variables: things that are not accessible, even dynamically, from outside that class or scope. I suspect this feature is intentionally left out of IDEs because it can’t be used safely the way that other “remove unused XXX” refactorings can.
