Pipe the package names from pip list --format=freeze (or pip freeze) into pip uninstall -y—for example, python -m pip freeze | xargs python -m pip uninstall -y on Linux/macOS or python -m pip freeze > req.txt && python -m pip uninstall -y -r req.txt && del req.txt on Windows—or, even cleaner and faster when working inside a virtual environment, delete and recreate the .venv folder directly.

Basic Example

# 1. Linux / macOS / Git Bash / WSL (inside your active virtual environment)
python -m pip freeze | cut -d '@' -f 1 | xargs -r python -m pip uninstall -y

# 2. Windows Command Prompt (cmd.exe)
python -m pip freeze > requirements_temp.txt && python -m pip uninstall -y -r requirements_temp.txt && del requirements_temp.txt

# 3. Windows PowerShell
python -m pip freeze | ForEach-Object { ($_ -split '==|@')[0] } | xargs python -m pip uninstall -y

# 4. Fastest & cleanest alternative for virtual environments: Recreate the venv
deactivate
rm -rf .venv && python -m venv .venv   # Windows: rmdir /s /q .venv && python -m venv .venv

Key Details & Edge Cases

  • Never run global bulk uninstall against your OS system Python: On Debian/Ubuntu (apt), Fedora (dnf), or macOS system Python, uninstalling packages globally can break OS utilities (apt, cloud-init, system scripts) or fail with error: externally-managed-environment (PEP 668). Always verify which python / where python points to your virtual environment before running bulk uninstalls—or pass --user (python -m pip freeze --user) if you only want to remove user-level (~/.local) packages.
  • Handling editable (-e) and direct URL/VCS (pkg @ git+https://...) entries:
    • pip freeze outputs editable installs as -e git+...#egg=pkg or -e /path/to/repo, and direct URL installs as pkg @ https://....
    • Writing to a temporary file and passing -r requirements_temp.txt (python -m pip freeze > req.txt && python -m pip uninstall -y -r req.txt) handles standard ==, direct URL @, and editable -e lines natively across every OS without needing cut or sed text parsing.
  • Exclude pip, setuptools, and wheel so your environment stays usable:
    • pip freeze omits pip, setuptools, and wheel by default (unless --all is passed), whereas pip list --format=freeze includes them. If you accidentally uninstall pip itself inside a virtual environment, restore it immediately with python -m ensurepip --upgrade.
  • Empty package list safety (xargs -r on Linux / if guard on Windows): If no third-party packages are installed, pip freeze outputs nothing; passing empty stdin to pip uninstall -y without -r (GNU xargs --no-run-if-empty) prints ERROR: You must give at least one requirement to uninstall. Using -r req.txt or xargs -r avoids that error cleanly.
  • Pure-Python cross-platform one-liner (works identically on Windows, macOS, and Linux):
    python -c "import subprocess, sys; pkgs = [p.split('==')[0].split(' @ ')[0] for p in subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'], text=True).splitlines() if p and not p.startswith('-e')]; pkgs and subprocess.run([sys.executable, '-m', 'pip', 'uninstall', '-y', *pkgs], check=True)"
    

Comparison of Bulk pip Package Removal Methods

Approach Platform Handles URL (@) & Editable (-e) Installs? Speed Best Used When
pip freeze > req.txt && pip uninstall -y -r req.txt All OS (Windows, macOS, Linux) Yes (handled natively by -r) Moderate (uninstalls one-by-one) Cross-platform removal when you must preserve the existing Python environment.
pip freeze | xargs -r pip uninstall -y Linux / macOS / WSL Needs cut -d '@' -f 1 for @ URLs Moderate Quick one-liner in Unix shells inside an active virtual environment.
pip freeze --user + uninstall All OS Yes (via -r) Moderate Cleaning up packages installed globally into your user site-packages (--user).
Delete & recreate .venv (rm -rf .venv) All OS Yes (wipes everything cleanly) Instant ($O(1)$ directory delete) Resetting a local project virtual environment to a guaranteed clean state.