Python
Pip
Programming
Package Management
Tech Troubleshooting

How to uninstall all packages installed by pip

Codemia Knowledge Hub ·

To uninstall every pip-installed package, run pip freeze | xargs pip uninstall -y. It lists every installed package and passes the list to pip uninstall with automatic confirmation. Depending on the situation, deleting the virtual environment or using pip-autoremove can be the better choice.

pip freeze piped to pip uninstall

bash
pip freeze | xargs pip uninstall -y
  1. pip freeze outputs every installed package in package==version format
  2. xargs passes each line as an argument to pip uninstall
  3. -y auto-confirms every uninstall prompt

On Windows (PowerShell)

PowerShell has no xargs by default. Use:

powershell
pip freeze | ForEach-Object { pip uninstall -y $_ }

Or go through a requirements file:

powershell
pip freeze > to_remove.txt
pip uninstall -y -r to_remove.txt
del to_remove.txt

With a requirements file (any OS)

bash
pip freeze > packages.txt
pip uninstall -y -r packages.txt
rm packages.txt

-r makes pip read package names from a file, just like pip install -r requirements.txt.

Deleting the virtual environment instead

Inside a virtual environment, the simplest reset is to delete it and create a fresh one:

bash
1# Deactivate first
2deactivate
3
4# Remove the environment
5rm -rf venv/
6
7# Create a fresh one
8python -m venv venv
9source venv/bin/activate  # or venv\Scripts\activate on Windows
10
11# Reinstall only what you need
12pip install -r requirements.txt

This guarantees no leftover files, cached data or partially installed packages. It is the better choice when you want a completely clean slate, suspect corrupted installs, are resetting a development environment, or have accumulated experimental packages over time.

Removing one package and its orphaned dependencies

To remove a package plus the dependencies nothing else needs, use pip-autoremove:

bash
1pip install pip-autoremove
2
3# Remove a package and its orphaned dependencies
4pip-autoremove requests -y
5
6# List packages that would be removed (dry run)
7pip-autoremove requests --list

It is the pip equivalent of apt autoremove on Debian/Ubuntu.

Keeping pip, setuptools and wheel

To remove almost everything but keep the package tooling:

bash
# Remove everything except pip, setuptools, and wheel
pip freeze | grep -v "^pip==" | grep -v "^setuptools==" | grep -v "^wheel==" | xargs pip uninstall -y

On Windows PowerShell:

powershell
pip freeze | Where-Object { $_ -notmatch "^(pip|setuptools|wheel)==" } | ForEach-Object { pip uninstall -y $_ }

Using pipx for command-line tools

Tools like black, flake8 or httpie installed with pip are better managed by pipx, which gives each tool its own isolated environment:

bash
1pip install pipx
2
3# Install tools in isolation
4pipx install black
5pipx install flake8
6
7# Remove a specific tool and its dependencies
8pipx uninstall black
9
10# Remove all pipx-managed tools
11pipx uninstall-all

This keeps tools from polluting project environments in the first place.

Comparing the methods

MethodScopeRemoves dependenciesCross-platformLeaves pip intact
pip freeze | xargs pip uninstall -yAll packagesN/A (removes everything)Linux/MacYes
Requirements file approachAll packagesN/A (removes everything)YesYes
Delete virtual environmentAll packages + envYes (everything gone)YesN/A (env deleted)
pip-autoremoveOne package + orphansYesYesYes
Filtered pip freezeAll except specifiedN/ALinux/MacYes (if excluded)

Why a second pass is sometimes needed

Some packages have circular dependencies or install hooks that reinstall dependencies during uninstallation, so the first pass may miss some. Run it again and verify:

bash
1# First pass
2pip freeze | xargs pip uninstall -y
3
4# Second pass to catch stragglers
5pip freeze | xargs pip uninstall -y
6
7# Verify
8pip list

Anything still listed after two passes is usually installed at the system level or ships with the Python distribution itself (like pip, setuptools and wheel).

Do not mass-uninstall from system Python

On Linux, system Python often has packages installed by the OS package manager (apt, yum, dnf). Removing them with pip can break system tools.

bash
# DANGEROUS: Do not run this on system Python
sudo pip freeze | xargs sudo pip uninstall -y
# This can break system tools like apt, yum, or system scripts

Check which Python you are on:

bash
1which python3
2# /usr/bin/python3  <-- system Python
3
4# vs a virtual environment
5# /home/user/project/venv/bin/python3  <-- safe to clean

If which python points to /usr/bin/python or /usr/local/bin/python, you are likely on system Python. Create a virtual environment before installing packages:

bash
python3 -m venv myenv
source myenv/bin/activate
# Now pip operations are isolated

Mistakes to avoid

  • Leaving off -y. pip then asks for confirmation on every package, which with 100+ packages means pressing y and Enter 100+ times.
  • Skipping verification. Circular dependencies can block some removals on the first pass, so run pip freeze again afterward.
  • Assuming pip freeze shows everything. Packages installed with --editable (development mode) or from direct file paths may not appear. Use pip list for a complete view.
  • Deleting the wrong virtual environment. Double-check the path before rm -rf; there is no undo.
  • No backup. Export your current state with pip freeze > backup_requirements.txt first so you can restore it.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms