Python Uninstall/Remove All Packages Installed By pip

Let’s look at a couple of quick ways to uninstall all the packages that we installed with pip in our environment or system.

First of all, to list all the packages that were installed via pip (in requirements format):

$ pip freeze

If we can list all the packages installed, then we can pass that list to pip uninstall to uninstall all of them:

$ pip uninstall -y -r <(pip freeze) # <() is known as process substitution
Uninstalling...

You could of course save the list to a separate file if you wanted to:

$ pip freeze > requirements.txt
$ pip uninstall -y -r requirements.txt

Using xargs also works:

$ pip freeze | xargs pip uninstall -y

If you have packages installed via VCS (Github, GitLab, Bitbucket, etc.), then you will have to uninstall them manually. Although make sure while deleting the non-VCS packages, you exclude the VCS ones from the pip freeze list so that pip uninstall doesn’t complain. Exclusion can be done with grep -v PATTERN (feel free to chose the search pattern accordingly):

# List all packages and filter those without "git+"
$ pip freeze | grep -v 'git+' | xargs pip uninstall -y

Tip: If your packages are installed in site.USER_SITE (user-site), then you can use the --user option to list packages in all the examples above:

# To list packages from user-site only
$ pip freeze --user

# Sample usage for uninstallation:
$ pip freeze --user | xargs pip uninstall -y

Leave a Reply

Your email address will not be published. Required fields are marked *