skillfed

autoflake

Removes unused imports and unused variables

autoflake v2.3.3 6.5M downloads/30d#1,900 on PyPI953
Permissive license MIT Active released

What it is and what it does

autoflake is a command-line tool that automatically detects and removes unused imports and variables from Python source files. It uses pyflakes to identify dead code and by default only removes unused imports from the standard library, leaving third-party imports untouched unless explicitly configured. This conservative default prevents accidental removal of imports that may have side effects.

The tool is typically used as a standalone formatter or integrated into pre-commit hooks to enforce code cleanliness. It supports configuration via pyproject.toml or setup.cfg, can operate on single files or recursively through directories, and offers fine-grained control over what gets removed—from all unused imports to unused variables to duplicate dictionary keys. It also removes useless pass statements by default.

Use it for:

  • Run as a pre-commit hook to automatically clean up unused imports before code review.
  • Integrate into CI/CD pipelines to check for unused code and fail builds when cleanup is needed.
  • Refactor legacy Python codebases to remove accumulated dead imports and variables.
  • Configure with --imports to safely remove unused third-party imports in specific projects.
  • Use --expand-star-imports to replace wildcard imports with explicit names for clarity.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

autoflake removes unused imports and unused variables from Python code by analyzing it with pyflakes, and optionally removes useless pass statements.

Yes. autoflake is a lightweight, actively maintained tool with no security vulnerabilities, minimal dependencies, and a permissive MIT license. It solves a real code-quality problem with low friction and is well-suited for both individual developers and teams integrating it into CI/CD workflows.

Install

autoflake on PyPI

pip

pip install autoflake

uv

uv add autoflake

poetry

poetry add autoflake

Installing autoflake

Before you install

Low install friction with only 2 runtime dependencies (pyflakes and tomli). The project is actively maintained with a recent release on 2026-02-20 and steady commit activity.

License in practice

MIT license is permissive, allowing use in commercial and private projects with minimal restrictions.

Quickstart

pip install autoflake

autoflake --in-place --remove-unused-variables example.py

Requires Python >=3.10.

Verify before relying

  • Whether the tool safely handles imports with side effects beyond the documented --imports and # noqa mechanisms.
  • Performance characteristics when processing large codebases or using parallel jobs (-j flag).

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 2 — pyflakes, tomli
Maintenance actively maintained — 175 days since the last release
Last repo commit
First released
Downloads 6,494,194/month — #1,900 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: autoflake-2.3.3-py3-none-any.whl

Keywords: automatic, clean, fix, import, unused

Environment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyTopic :: Software Development :: Quality Assurance

Tags

remove unused imports pythonclean up unused variablespython code cleanup toolautomate import removalunused code detectionpython linting automationcode quality cleanup
code-cleanuppre-commit-readylinting

More Quality Assurance packages