1306

How do I uninstall all packages installed by pip from my currently activated virtual environment?

2
  • 62
    @patelshahrukh uninstalling python DOES NOT remove pip packages. please AVOID doing that, since it both most likely WON'T WORK the way you think it will, and, depending on how you install python again, can leave your machine in an unstable state that's more work to fix. Commented Apr 23, 2018 at 18:23
  • 2
    this might help for packages installed in development mode or editable mode: stackoverflow.com/questions/17346619/… Commented May 2, 2020 at 16:21

34 Answers 34

1841

I've found this snippet as an alternative solution. It's a more graceful removal of libraries than remaking the virtualenv:

pip freeze | xargs pip uninstall -y

In case you have packages installed via VCS, you need to exclude those lines and remove the packages manually (elevated from the comments below):

pip freeze --exclude-editable | xargs pip uninstall -y

If you have packages installed directly from github/gitlab, those will have @. Like:

django @ git+https://github.com/django.git@<sha>

You can add cut -d "@" -f1 to get just the package name that is required to uninstall it.

pip freeze | cut -d "@" -f1 | xargs pip uninstall -y
26
  • 15
    I find this a good solution, purely because it doesn't remove the virtual environment entirely - I may have made changes to e.g. postactivate which will remain. Commented Jul 4, 2013 at 8:17
  • 31
    You can also use pip freeze --exclude-editable | xargs pip uninstall -y to ignore VCS packages without using a grep pattern Commented Jul 26, 2018 at 21:15
  • 56
    ERROR: Invalid requirement: '@' I have packages that ive installed from my gitlab.
    – mRyan
    Commented Jun 5, 2020 at 12:22
  • 33
    pip freeze lists all packages, including those installed in the OS. User will is unable (and possibly does not want) to remove them without root permissions. pip freeze --user worked for me. Commented Jun 12, 2020 at 16:15
  • 18
    @mRyan pip list --format=freeze |xargs pip uninstall -y did the trick for me
    – skynet1010
    Commented Mar 23, 2022 at 19:41
934

This will work for all Mac, Windows, and Linux systems. To get the list of all pip packages in the requirements.txt file (Note: This will overwrite requirements.txt if exist else will create the new one, also if you don't want to replace old requirements.txt then give different file name in the all following command in place requirements.txt).

pip freeze > requirements.txt

Now to remove one by one

pip uninstall -r requirements.txt

If we want to remove all at once then

pip uninstall -r requirements.txt -y

If you're working on an existing project that has a requirements.txt file and your environment has diverged, simply replace requirements.txt from the above examples with toberemoved.txt. Then, once you have gone through the steps above, you can use the requirements.txt to update your now clean environment.

And For single command without creating any file as @joeb suggested

pip uninstall -y -r <(pip freeze)
14
  • 28
    probably worth mentioning that you are force overwriting their requirements.txt file, in case they didn't know. :) Commented Feb 18, 2017 at 0:24
  • 7
    In addition, if one wants to remove all packages, appending "-y" will do so. Example: pip uninstall -r requirements.txt -y Commented Apr 9, 2017 at 0:13
  • 164
    You can use pip uninstall -y -r <(pip freeze) to do everything in one go.
    – joebeeson
    Commented Jun 8, 2017 at 14:21
  • 1
    @joeb yes we can do that way also. Commented Jun 9, 2017 at 9:28
  • 1
    @HarshadKavathiya you can use another file to store pip freeze, like "current.txt", so requirements.txt can stay untouched cheers
    – katsos
    Commented May 11, 2018 at 20:28
234

I wanted to elevate this answer out of a comment section because it's one of the most elegant solutions in the thread. Full credit for this answer goes to @joeb.

pip uninstall -y -r <(pip freeze)

This worked great for me for the use case of clearing my user packages folder outside the context of a virtualenv which many of the above answers don't handle.

Edit: Anyone know how to make this command work in a Makefile?

Bonus: A bash alias

I add this to my bash profile for convenience:

alias pipuninstallall="pip uninstall -y -r <(pip freeze)"

Then run:

pipuninstallall

Alternative for Pipenv

If you are using pipenv, you can run:

pipenv uninstall --all

Alternative for Poetry

If you are using Poetry, run:

poetry env remove --python3.9

(Note that you need to change the version number there to match whatever your Python version is.)

7
  • I like it but it doesn't work in the null case (pip freeze results in nothing output if no packages installed, and then pip uninstall complains, unfortunately).
    – Eric G
    Commented Aug 19, 2018 at 23:53
  • Hmm good catch. Perhaps it could be wrapped into a bash function that checks whether the pip freeze output is non-empty. I don't see a great way to achieve that while keeping the command a nice short one-liner. Commented Aug 20, 2018 at 17:05
  • 2
    make uses sh by default, but the substitution syntax <(...) is a bashism. So you can either use bash -c "...", or work around by doing a pip freeze | pip uninstall -r /dev/stdin
    – Caesar
    Commented Oct 8, 2018 at 23:45
  • 2
    Does not work with Azure Notebooks: ERROR: Cannot uninstall 'bitarray'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
    – SeaDude
    Commented Apr 30, 2020 at 7:03
  • 1
    Does not works on Windows The system cannot find the file specified. Commented Nov 2, 2022 at 2:01
160

This works with the latest. I think it's the shortest and most declarative way to do it.

virtualenv --clear MYENV

But why not just delete and recreate the virtualenv?

Immutability rules. Besides it's hard to remember all those piping and grepping the other solutions use.

4
  • 10
    Is this effectively the same as running wipeenv? virtualenvwrapper.readthedocs.org/en/latest/… Commented Apr 15, 2015 at 16:11
  • Actually — it seems (from what I just ran into) whereas wipeenv while within the environment throws an error and doesn't remove anything if used in the context of a pip install -e development build, attempting to use virtualenv --clear MYENV doesn't throw an error and removes none of the packages that you may have installed previously in the environment. At least this is the case on OSX. See bitbucket.org/dhellmann/virtualenvwrapper/issues/211/… for further info.
    – mpacer
    Commented Aug 4, 2015 at 6:20
  • 5
    wipeenv is an alias provided by virtualenvwrapper, so not everyone has it. Commented Oct 11, 2017 at 17:29
  • 4
    well, this is kind of a dirty trick, but works like magic. I would prefer that everyone to use pip uninstall -r requirements.txt -y. It makes a great clean up.
    – Muema
    Commented Mar 9, 2018 at 19:25
79

I managed it by doing the following:

  1. Create the requirements file called reqs.txt with currently installed packages list
pip freeze > reqs.txt
  1. Then uninstall all the packages from reqs.txt
# -y means remove the package without prompting for confirmation
pip uninstall -y -r reqs.txt

I like this method as you always have a pip requirements file to fall back on should you make a mistake. It's also repeatable, and it's cross-platform (Windows, Linux, MacOs).

0
67

Other answers that use pip list or pip freeze must include --local else it will also uninstall packages that are found in the common namespaces.

So here are the snippet I regularly use

 pip freeze --local | xargs pip uninstall -y

Ref: pip freeze --help

2
  • This worked until it hit a package that produced this error: ERROR: Cannot uninstall 'bitarray'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.. Then I couldn't uninstall any more modules.
    – SeaDude
    Commented Apr 30, 2020 at 7:02
  • This will include the packages from distribution repositiories. The --user from stackoverflow.com/a/60966049/721644 then comes very handy despite the complaints in the first comment there. But for some reason even that included packages installed in /usr/li64/... even though the number of packages was much smaller. Commented Mar 25 at 10:16
46

Method 1 (with pip freeze)

pip freeze | xargs pip uninstall -y

Method 2 (with pip list)

pip list | awk '{print $1}' | xargs pip uninstall -y

Method 3 (with virtualenv)

virtualenv --clear MYENV
2
  • 9
    Method 2 (pip list) works great until you have pip accidentally uninstall itself -_-
    – Justin
    Commented Feb 24, 2017 at 0:48
  • Method 2 didn't work in my case because there is a header in the list which needs to be ignored. This one worked: pip list | awk '{print $1}' | grep -vE "^pip$|^Package$|^---" | xargs pip uninstall -y
    – Menda
    Commented Feb 21, 2022 at 16:32
42

On Windows if your path is configured correctly, you can use:

pip freeze > unins && pip uninstall -y -r unins && del unins

It should be a similar case for Unix-like systems:

pip freeze > unins && pip uninstall -y -r unins && rm unins

Just a warning that this isn't completely solid as you may run into issues such as 'File not found' but it may work in some cases nonetheless

EDIT: For clarity: unins is an arbitrary file which has data written out to it when this command executes: pip freeze > unins

That file that it written in turn is then used to uninstall the aforementioned packages with implied consent/prior approval via pip uninstall -y -r unins

The file is finally deleted upon completion.

0
37

Best way to remove all packages from the virtual environment.

Windows PowerShell:

pip freeze > unins ; pip uninstall -y -r unins ; del unins

Windows Command Prompt:

pip freeze > unins && pip uninstall -y -r unins && del unins

Linux:

pip3 freeze > unins ; pip3 uninstall -y -r unins ; rm unins
3
  • Windows no such option: -y ran without -y option but it didn't delete any packages
    – flywire
    Commented Jan 29, 2023 at 1:01
  • @flywire it worked for me in votes and see the votes. FYI, I tried this windows 10 and windows 11 operating systems. Commented Jan 29, 2023 at 16:11
  • It is not Windows it is PowerShell. Windows - cmd: &, PowerShell: ;
    – flywire
    Commented Jan 29, 2023 at 23:56
34

I use the --user option to uninstall all the packages installed in the user site.

pip3 freeze --user | xargs pip3 uninstall -y
5
  • I believe this answer doesn't add much new information, I would rather have suggested an improvement to another already existing similar answer such as this one: stackoverflow.com/a/45475070/11138259
    – sinoroc
    Commented Apr 3, 2020 at 14:15
  • 1
    If you are using a virtualenv and get ERROR: You must give at least one requirement to uninstall, remove the --user part
    – Checo R
    Commented Nov 29, 2020 at 20:13
  • pip3 freeze | xargs pip3 uninstall -y ==> ` PermissionError: [Errno 13] Permission denied: '/usr/local/bin/ap' -> '/tmp/pip-uninstall-q9gzbj0d/ap'`
    – SL5net
    Commented Feb 28, 2021 at 12:48
  • 1
    @SL5net. You might need to run it as superuser. Something like sudo sh -c 'pip3 freeze | xargs pip3 uninstall -y' (The added sh -c and quotes are because pipe doesnt tend to play nicely with sudo) Or you could just do your command as root, but I dont recomend that as its not a great habit to be in. shelling about in root makes it pretty easy to accidently murder your system, trust me, bitter experience talking here.
    – Shayne
    Commented Jun 15, 2021 at 13:32
  • 1
    Good point of --user option. Because --local lists all packages installed locally, not user ones. Commented Jan 3, 2023 at 13:57
24

For Windows users, this is what I use on Windows PowerShell

 pip uninstall -y (pip freeze)
0
20

First, add all package to requirements.txt

pip freeze > requirements.txt

Then remove all

pip uninstall -y -r requirements.txt 
0
20

The quickest way is to remake the virtualenv completely. I'm assuming you have a requirements.txt file that matches production, if not:

# On production:
pip freeze > reqs.txt

# On your machine:
rm $VIRTUALENV_DIRECTORY
mkdir $VIRTUALENV_DIRECTORY
pip install -r reqs.txt
1
  • 2
    Does this even handle the case where there was a editable install (basically a setuptools develop mode install) that created a local .egg-info file that then interfered with the rest of the installation/uninstallation process? Since it's a set of files it doesn't seem to know how to handle their presence, and rather than uninstalling anything it makes a local directory structure under MYENV complete with: ` > New python executables in MYENV/bin/python3.4 > Also creating executable in MYENV/bin/python > Installing setuptools, pip, wheel...done.` But MYENV hasn't reset the environment!
    – mpacer
    Commented Aug 4, 2015 at 6:35
17

Using virtualenvwrapper function:

wipeenv

See wipeenv documentation

1
  • 6
    If you are using virtualenvwrapper, type wipeenv
    – raratiru
    Commented Oct 24, 2016 at 19:22
14

Its an old question I know but I did stumble across it so for future reference you can now do this:

pip uninstall [options] <package> ...
pip uninstall [options] -r <requirements file> ...

-r, --requirement file

Uninstall all the packages listed in the given requirements file. This option can be used multiple times.

from the pip documentation version 8.1

10
pip uninstall `pip freeze --user`

The --user option prevents system-installed packages from being included in the listing, thereby avoiding /usr/lib and distutils permission errors.

1
7

(adding this as an answer, because I do not have enough reputation to comment on @blueberryfields 's answer)

@blueberryfields 's answer works well, but fails if there is no package to uninstall (which can be a problem if this "uninstall all" is part of a script or makefile). This can be solved with xargs -r when using GNU's version of xargs:

pip freeze --exclude-editable | xargs -r pip uninstall -y

from man xargs:

-r, --no-run-if-empty

If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.

7
pip3 freeze --local | xargs pip3 uninstall -y

The case might be that one has to run this command several times to get an empty pip3 freeze --local.

1
  • This breaks on editably installed packages right now Commented Oct 21, 2022 at 4:22
3

This was the easiest way for me to uninstall all python packages.

from pip import get_installed_distributions
from os import system
for i in get_installed_distributions():
    system("pip3 uninstall {} -y -q".format(i.key))
3

This works on my windows system

pip freeze > packages.txt && pip uninstall -y -r packages.txt && del packages.txt

The first part pip freeze > packages.txt creates a text file with list of packages installed using pip along with the version number

The second part pip uninstall -y -r packages.txt deletes all the packages installed without asking for a confirmation prompt.

The third part del packages.txt deletes the just now created packages.txt.

2

Cross-platform support by using only pip:

#!/usr/bin/env python

from sys import stderr
from pip.commands.uninstall import UninstallCommand
from pip import get_installed_distributions

pip_uninstall = UninstallCommand()
options, args = pip_uninstall.parse_args([
    package.project_name
    for package in
    get_installed_distributions()
    if not package.location.endswith('dist-packages')
])

options.yes = True  # Don't confirm before uninstall
# set `options.require_venv` to True for virtualenv restriction

try:
    print pip_uninstall.run(options, args)
except OSError as e:
    if e.errno != 13:
        raise e
    print >> stderr, "You lack permissions to uninstall this package.
                      Perhaps run with sudo? Exiting."
    exit(13)
# Plenty of other exceptions can be thrown, e.g.: `InstallationError`
# handle them if you want to.
2

the easy robust way cross-platform and work in pipenv as well is:

pip freeze 
pip uninstall -r requirement

by pipenv:

pipenv run pip freeze 
pipenv run pip uninstall -r requirement

but won't update piplock or pipfile so be aware

2

Why not just rm -r .venv and start over?

1
  • I am not very experienced in Python but this is what I had in mind too.
    – havryliuk
    Commented Sep 21, 2022 at 7:13
2

On Windows if your path is configured correctly, you can use:

pip freeze > unins && pip uninstall -y -r unins && del unins
1

For Windows, using command prompt, deleting all the installed packages in a virtual environment (after the environment is active):

for /f %i in ('pip freeze --local') do pip uninstall -y %i
0

If you're running virtualenv:

virtualenv --clear </path/to/your/virtualenv>

for example, if your virtualenv is /Users/you/.virtualenvs/projectx, then you'd run:

virtualenv --clear /Users/you/.virtualenvs/projectx

if you don't know where your virtual env is located, you can run which python from within an activated virtual env to get the path

0

In Command Shell of Windows, the command pip freeze | xargs pip uninstall -y won't work. So for those of you using Windows, I've figured out an alternative way to do so.

  1. Copy all the names of the installed packages of pip from the pip freeze command to a .txt file.
  2. Then, go the location of your .txt file and run the command pip uninstall -r *textfile.txt*
0

If you are using pew, you can use the wipeenv command:

pew wipeenv [env]

0

I simply wanted to remove packages installed by the project, and not other packages I've installed (things like neovim, mypy and pudb which I use for local dev but are not included in the app requirements). So I did:

cat requirements.txt| sed 's/=.*//g' | xargs pip uninstall -y

which worked well for me.

0

Select Libraries To Delete From This Folder:

C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages

1
  • 1
    Although this does delete all packages (and more), it doesn't do so with pip as the question asked. Commented Dec 13, 2021 at 3:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.