This is available now with a new release of the pylance extension (which I assume most people using python in VS Code will have).

It should be noted that the optimizeImports with the ctrl + alt/option + o keybinding only sorts and does not remove unused imports (see github issue).

I have autosave in place, so I prefer a keyboard shortcut. If you have the pylance extension, you can add the following to your keybindings.json

    {
        "key": "shift+alt+r",
        "command": "editor.action.codeAction",
        "args": {
            "kind": "source.unusedImports",
        }
    }

You can change the key binding to be whatever you want, but basically when you press the keybinding (i.e shift + option/alt + r) it should remove all your unused imports.

I believe if you wanted this automatically on save as above you could add the following into your settings.json:

    "[python]": {
        "editor.codeActionsOnSave": {
          "source.organizeImports": "explicit",
          "source.unusedImports": "explicit",
        }
    }
Answer from emv on Stack Overflow
🌐
PyPI
pypi.org › project › autoflake
autoflake · PyPI
autoflake removes unused imports and unused variables from Python code.
      » pip install autoflake
    
Published   Feb 20, 2026
Version   2.3.3
Top answer
1 of 11
43

This is available now with a new release of the pylance extension (which I assume most people using python in VS Code will have).

It should be noted that the optimizeImports with the ctrl + alt/option + o keybinding only sorts and does not remove unused imports (see github issue).

I have autosave in place, so I prefer a keyboard shortcut. If you have the pylance extension, you can add the following to your keybindings.json

    {
        "key": "shift+alt+r",
        "command": "editor.action.codeAction",
        "args": {
            "kind": "source.unusedImports",
        }
    }

You can change the key binding to be whatever you want, but basically when you press the keybinding (i.e shift + option/alt + r) it should remove all your unused imports.

I believe if you wanted this automatically on save as above you could add the following into your settings.json:

    "[python]": {
        "editor.codeActionsOnSave": {
          "source.organizeImports": "explicit",
          "source.unusedImports": "explicit",
        }
    }
2 of 11
31
  • Confirm you have Pylance enabled in extensions. It comes with the vscode Python extension by Microsoft.

  • In your .vscode/settings.json, add this

    "python.analysis.fixAll" : ["source.unusedImports"]

Now when you do Ctrl+Shift+P (Comand Palette) -> "Fix All" this will clean up the unused imports.

  • To auto cleanup on save, add this:
    "editor.codeActionsOnSave": {
        "source.unusedImports": true,
    }

you can also add "source.organizeImports": true in there to sort imports on save.

  • If this doesnt work try overriding any other installed language servers with:
    "python.languageServer": "Pylance",

Docs here

🌐
Reddit
reddit.com › r/learnpython › remove unused imports
r/learnpython on Reddit: Remove unused imports
April 11, 2022 -

I am working on a codebase that is written by multiple people in our organisation. I can see some files where some functions are imported but were never used. Now, since I am not much experienced with python, I just need some guidance regarding whether it is perfectly fine to remove them or is there a possibility that it can be a hack for some use cases ? If later is possible, please mention those cases.

🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 115000035164-Unconfigurable-Auto-Remove-of-Unused-Python-Imports
Unconfigurable Auto Remove of Unused Python Imports – IDEs Support (IntelliJ Platform) | JetBrains
January 16, 2017 - There is a checkbox for it in "Reformat File" dialog (Ctrl+Alt+Shift+L). Optimize Imports is also run during some refactorings like "Move..." to remove obsolete imports. ... Thanks I believe that was it.
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Autoflake - Remove unused Python imports - Visual Studio Marketplace
January 25, 2021 - Extension for Visual Studio Code - Remove unused and re-organize imports in Python
🌐
Reddit
reddit.com › r/python › automatically find and remove unused import statements in your project.
r/Python on Reddit: Automatically find and remove unused import statements in your project.
June 15, 2022 -

Hello everyone, I am honored to introduce a very useful tool to you

Unimport a linter & formatter for finding/removing unused import statements. It includes a CLI interface, a static analysis layer (using Python's AST module), and a fully-fledged refactoring engine (based on LibCST). Downloaded over 100k+ and battle-tested by hundreds of projects.

You can customize Unimport before using it, if you want, you can use it with pre-commit or in the docker environment. It supports Python 3.6, 3.7, 3.8, .3.9, and 3.10 versions.

Here is the documentation -> https://unimport.hakancelik.dev/
Here is the Github address -> https://github.com/hakancelikdev/unimport

Unimport

I am forever open to your suggestions and any kind of contribution.

🌐
GitHub
gist.github.com › jvacek › efe6b6fec1bbeb25718bc89b9c1b5acc
Remove unused python imports on save in VSCode · GitHub
//Remove Unused Python imports via bash "emeraldwalk.runonsave": { "commands": [ { "match": "\\.py$", "isAsync": true, // Use the full path from above in case it can't find it "cmd": "autoflake --in-place --remove-all-unused-imports \"${file}\"" }, ] },
Find elsewhere
🌐
Astral
docs.astral.sh › ruff › rules › unused-import
unused-import (F401) | Ruff
The fix offered depends on the type of the unused import. Ruff will suggest a safe fix to export first-party imports with either a redundant alias or, if already present in the file, an __all__ entry. If multiple __all__ declarations are present, Ruff will not offer a fix. Ruff will suggest an unsafe fix to remove third-party and standard library imports -- the fix is unsafe because the module's interface changes.
🌐
Plain English
plainenglish.io › home › blog › python › autoflake — remove unused imports & unused variables from python code
Autoflake — Remove Unused Imports & Unused Variables from Python Code
January 4, 2021 - positional arguments: files files to format optional arguments: -h, --help :show this help message and exit -c, --check :return error code if changes are needed -i, --in-place :make changes to files instead of printing diffs -r, --recursive :drill down directories recursively --exclude globs :exclude file/directory names that match these comma-separated globs --imports IMPORTS :by default, only unused standard library imports are removed; specify a comma-separated list of additional modules/packages --expand-star-imports:expand wildcard star imports with undefined names; this only triggers if
🌐
GitHub
github.com › PyCQA › isort › issues › 1105
Delete unused imports · Issue #1105 · PyCQA/isort
January 17, 2020 - Delete unused imports#1105 · Copy link · Labels · enhancementNew feature or requestNew feature or request · astier · opened · on Jan 17, 2020 · Issue body actions · Would it be possible to delete unused import? Reactions are currently unavailable · No one assigned ·
Author   astier
🌐
DEV Community
dev.to › vivekjami › unused-imports-the-hidden-performance-tax-3340
Unused Imports - The Hidden Performance Tax - DEV Community
September 15, 2025 - # .github/workflows/import-hygiene.yml ... - name: Check unused imports run: | ruff check --select F401 . autoflake --check --remove-all-unused-imports -r ....
🌐
Python
bugs.python.org › issue35202
Issue 35202: Remove unused imports in standard library - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/79383
🌐
Avil Page
avilpage.com › 2014 › 08 › remove-unused-imports-variables-from.html
Auto Remove Python Unused Imports & Variables In Emacs | Avil Page
August 16, 2014 - ;; Py-rm - Remove unused variables & imports from python (defun pyrm () (interactive) (setq command (concatenate 'string "autoflake --in-place --remove-unused-variables " buffer-file-name)) (shell-command command) ;; Reload the modified file (revert-buffer t t) ) ;; set a custom key for pyrm (global-set-key [f8] 'pyrm)
🌐
Cursor
forum.cursor.com › support › help
Autoremove unused python imports - Help - Cursor - Community Forum
June 23, 2025 - I can’t seem to find this functionality in cursor. Can someone point it out to me? In vanilla vscode you can do this: "python.analysis.fixAll": [ "source.unusedImports" ],
🌐
GitHub
github.com › microsoft › pylance-release › issues › 3436
Pylance is eagerly and aggressively removing unused imports when saving a module · Issue #3436 · microsoft/pylance-release
October 5, 2022 - Python version (& distribution if applicable, e.g. Anaconda): n/a · import os if __name__ == '__main__': print("Yo dude!") ... Watch as Pylance fixes your code by removing the unused import.
Author   jason-s
🌐
GitHub
github.com › PyCQA › autoflake
GitHub - PyCQA/autoflake: Removes unused imports and unused variables as reported by pyflakes · GitHub
autoflake removes unused imports and unused variables from Python code.
Starred by 952 users
Forked by 84 users
Languages   Python
🌐
Vim
vim.org › scripts › script.php
python_import.vim - automatically remove unused imports and clean up the rest : vim online
created by Jason Lunz script type ftplugin description This plugin defines a function for automatically cleaning up the imports in a python module or program. It does two things: - removes unused imports - sorts and formats the remaining imports This function is mapped to <LocalLeader>i (for ...
🌐
GitHub
github.com › microsoft › pylance-release › issues › 1547
"Remove unused imports" removes all imports · Issue #1547 · microsoft/pylance-release
July 2, 2021 - Describe the bug Steps to Reproduce Type a few imports, like import array; import glob; import math Type print(math.cos(10)) In the line of import you can see a pattern, then select the "Remove unused imports" Actual behavior It removes ...
Author   ttSpace
🌐
Reddit
reddit.com › r › Python › comments › 2dqd7m › remove_unused_imports_variables_from_python
r/Python - Remove Unused Imports & Variables From Python
June 15, 2022 - Yeah, you can even remove unused imports with "CTRL + SHIFT + O" ... PyCharm too, but if you're up to some clever shenanigans with locals() or globals(), sometimes it gets it wrong.