How to prevent IntelliJ IDEA from deleting unused packages?
Asked Answered
M

7

77

I'm working with intelliJ and my problem is when I start to import some temporary unused packages into my class file intellij delete those line within a second.

how can I turn off this not so nice feature?

Mortie answered 22/6, 2012 at 11:0 Comment(0)
B
99

Disable File | Settings | Editor | General | Auto Import | Optimize imports on the fly.

Normally you don't need to add imports manually, IDEA does it for you.

Braden answered 22/6, 2012 at 11:3 Comment(6)
While this the common case in Java, with Scala and implicit conversions, things are not as smooth. For example, it keeps removing import scala.collection.JavaConverters._. But IntelliJ doesn't auto-import when I type .asScala at the end of an expression that returns a Java collection. So IMHO the option should be off by default for Scala code.Gnawing
For me, the Auto Import option was under Settings > Editor > General > Auto Import > Optimize imports on the Fly, as in @merrick's answer. v14.1.4Munshi
@Braden I'm using IDEA 15.0.1 Build 143.382 and do not have this enabled. However, IDEA removes my imports whenever I comment out lines. Any idea?Pauperize
how can I disable this for a single Kotlin file? The damn IDE is constantly inserting java String and Set imports into this one file and it produces error messages of course. Very annoying since it happens whenever imports are optimized for itPampas
Groovy seems to be affected as well. Some imports are marked as unused but they are required for compilation. I have noted this happens specially when using inner classes of an extended class.Monetmoneta
God forbid you have two packages with the same name!!! This "remove unused imports" feature drives me crazy. Auto add YES. But don't remove a package I already used IntelliJ. BTW, this is missing in IntelliJ Ultimate Golang plugin.Winonawinonah
V
17

For Scala developers: you can have enabled

Settings > Editor > General > Auto Import > Optimize imports on the fly

but add exclusions into

Settings > Editor > Code Style > Scala > Imports always marked as used:

enter image description here

Veronica answered 20/4, 2016 at 14:2 Comment(1)
Thank you! I didn't know that and that drove me crazy. Very useful for Scala specially when you start using a lot things like Cats or ScalaZ...Mattern
B
12

In Intellij 14 : Settings > Editor > General > Auto Import > Optimize imports on the fly.

Banksia answered 3/3, 2015 at 19:45 Comment(0)
G
3

(Update) For the Scala developers out there, you can have 1/2 the cake and eat the other 1/2 now: disable the auto-optimize for just scala

enter image description here

Gnawing answered 16/10, 2015 at 21:18 Comment(0)
O
1

For me while using Go, this was caused by optimizing imports on save. IntelliJ IDEA -> Prefernces -> Tools -> Actions on Save -> Optimize imports.

Otti answered 2/12, 2022 at 22:42 Comment(0)
K
0

The above answers are obviously the way to go. Here's another quick fix (a bad one at that) is to simply use it somewhere in your code

if (0) console.log(LibraryName.version);

This should only be a temporary measure though.

Krystlekrystyna answered 29/8, 2022 at 19:13 Comment(0)
A
0

I use this to prevent the package from being yeeted:

import {Dropdown} from 'bootstrap';
(() => typeof Dropdown)();
Alyce answered 16/7 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.