'How do I delete all unused imports in the active file with one command in VS Code (typescript)?
Previously, I used to be able to do CMD+SHIFT+p > Organize imports
and this would both sort and delete unused imports. This functionality seems to have broken.
How can I delete unused imports quickly with one command?
Current workaround is: click on unused import, CMD+. > Delete unused imports
.
Languages for which this is applicable (typescript, typescriptreact, javascript, javascriptreact).
I have confirmed VS code is using a recent version of typescript unlike people in this post
Solution 1:[1]
It turns out that Organize Imports
does actually work as expected but it was conflicting with the Deno
plugin.
I found this out by using the amazingly useful Extension Bisect feature which is built into Visual Studio Code. Extension Bisect disables half your extensions and asks you to check if the issue persists. This process is repeated until only a single extension is left. I strongly recommend trying it out via CMD+SHIFT+P > Start Extension Bisect
Turns out my issue is a duplicate of this GitHub issue.
Solution 2:[2]
There does not appear to be a built-in way to access the delete all imports
functionality. But you can install the Remove Unused Imports extension
VS Code extension to remove unused ES6 imports inside JavaScript and TypeScript files (
.js
,.jsx
,.ts
and.tsx
extensions) without changing the current order, as opposed to the built-in VS Code "Organize Imports" functionality.
and
try this keybinding:
{
"key": "cmd+shift+r", // or whatever you want
"command": "remove-unused-imports.main",
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |