Virtual Env : How to Clean up a Virtual Env?

Virtual Env Clean Feature

The ‘virtual env’ is an essential element while working on Python projects as it stores all the files and dependencies in one place for easy access and maintainability.  It becomes very important to have a well-organized virtual environment. So in this article, we will be discussing in detail the various ways in which you can maintain a clean and tidy virtual environment.

What is a ‘virtual env’, and why is it useful?

Consider a situation where you are working on a project requiring several dependencies and packages to be installed, and these requirements need to be stored in your local machine, which requires storage space, suppose all these requirements are scattered all over your computer, creating a mess all over, wouldn’t that be troublesome?

To resolve this issue, we try to create a virtual space on our computer that remains specific to the current project that we are working on, and there is no interference from the outside to this space and vice versa. This virtual space is nothing but a ‘virtual env’.

Creating a ‘virtual env’ is really useful as it acts as an actual machine for our projects, isolating it from our main machine and making things more efficient.

What does ‘clean up’ mean?

Cleaning up a virtual env means that if at all the need arises, we might have to organize and set up our virtual env in a proper manner for better efficiency.

The process of clean-up involves different tasks like getting rid of the unused and outdated packages that might no longer be useful for our project, keeping the packages up-to-date, and various others that we might be studying in-depth as we go ahead in this article.

Why Cleaning the virtual env is Important?

Accumulation of unnecessary packages and dependencies

While working on a project for a long time and developing the project into a giant one, there is a possibility that you might install many packages that might not be required for your project anymore, so you might want to get rid of these packages that are not required. In this scenario, cleanup is essential.

Effect on Performance and Disk Space

Getting rid of all the unnecessary stuff and keeping everything up-to-date will enhance and improve the performance and speed of your project by manifolds and also help in maintaining the disk space efficiently as storage space is a very important factor while working on huge projects.

Maintaining a Clean and Tidy Virtual Environment

Since we have been thinking of a situation in which we are working on a big project, and big projects are always handled in a team environment.

The code that you write and maintain will also be seen and used by others, and nobody wants to work in a shabby environment, so it is important that you maintain a clean and tidy environment so that people other than you can understand what is happening and get a better feel and experience.

Maintaining and organizing the virtual environment will not only help you but will also make the life of people on your team easier.

Exploring the virtual env.

Before starting with cleaning something, we must at least have an idea about it, so we would be taking an overview of some concepts related to virtual env that we will need to move ahead with this article.

To learn more about virtual env with Python, you can go through the linked article.

Activating the virtual env.

Activating the virtual env is a huge topic in itself and can be done by various methods. I would be linking to the official documentation of Python, where you could easily find how to create and activate a virtual env.

Creation of virtual env.

Enlist all the packages in our virtual env

To move ahead towards cleaning up, it is important for us to have an idea about what is happening in our environment and maybe have a look at all the stuff that is installed in our environment.

To check what is installed in our environment, we will have to navigate through our terminal to our project virtual env that we have already created. Once you have reached the desired location, you can run the below statement in the terminal.

pip list --local

Running the above statement without any errors will show you a list of all the installed packages in your virtual environment.

Package List inside virtual env
Package List Inside Virtual Env

Pip will generate a similar list of all the installed packages, as shown in the image above.

Methods of Cleaning ‘Virtual Env’

Since we’ve discussed all the factors related and understood the reasons why we would want to keep our virtual environment clean and also gained a bit of knowledge about what the virtual env is, and how it works so, let’s move on to actual ways by which we can ‘clean up’ of environment.

Manually Managing and Deleting File

In this method, you will have to manually navigate to your virtual env and manually identify the packages that you need and the ones that you don’t. You can keep the packages that you need and delete the ones that you don’t need.

This sound simple, but when talking about huge packages doing this would be a troublesome task. There must be a way that we can adapt to and try to make things a little easy for us. Let’s look at some better ways of managing the virtual env.

Using ‘pip freeze’

Pip is a package manager for Python files to know more about it check out this link.

Managing the virtual env can be done in two ways using pip freeze, which we will discuss in brief.

Make sure that you have the latest version of pip installed on your system.

Run the command below on your terminal to install the latest version of pip.

pip install --upgrade pip

Pip freeze > requirements.txt

To try this method, first, you must navigate to your terminal’s virtual env and type in the command below in your terminal.

pip freeze > requirements.txt

If done properly, this command will automatically go through your list of packages and develop a text file in your current directory. This text file will consist of all the essential dependencies of your project and ignore all the unnecessary ones.

Once this text file is ready, you can just simply refer to the list and delete the packages that are not mentioned in the requirements.txt file, which will eventually leave only the necessary packages in your virtual env deleting all the unnecessary ones.

Once you have figured out which packages are not required, you can run the below command on the same terminal.

pip uninstall <package_name>

Pip freeze > to-unistall.txt

Navigate to the virtual env in your terminal and run the below common.

pip freeze > to-uninstall.txt
pip uninstall -r to-uninstall.txt

The above command goes through your packages and identifies which ones are not required for your package or, in other words, determines the unnecessary packages.

The first line of the command does the work of determining and making a list of packages not required for our project and writes it to a text file which is the ‘to-unistall.txt’ in your current directory.

The second line of this command goes to the ‘to-unistall.txt’ file, reads it, and automatically deletes all the packages mentioned in this text file.

This could be one of the best methods to delete all the packages that are not required in one go.

Deleting Temporary Files and Cache

As the project goes on and increases in scale, it tends to develop temporary files and also some cache which occupies our disk storage without any reason and slows down the overall performance, but if there is a problem, we definitely also have a solution to it.

Let’s take a look at the solution to this problem.

First, navigate to your virtual env on your terminal, then run the below code on your terminal.

pip cache purge

Running the above code will automatically delete all the temporary files and cache that have been developed by the packages.

Doing this will improve your project’s storage efficiency and clear all the unnecessary occupied disk space, making a place for important packages to come in.

Updating Outdated Packages

With developing technologies, the packages that we use also tend to get outdated very fast, and since we are talking about maintaining our virtual env it is important that our environment has all the packages that are up-to-date and nothing is stale.

To do this, we will first have to get into our virtual-env through our terminal and run the below-given command.

pip list --outdated

Running the above command will display a list of all the outdated packages in your environment, once you have the list, you can easily go and update all these one by one using the command below.

pip install --upgrade <package_name>

Following the above steps will help you keep all the packages up-to-date in your environment.

Using ‘pip autoremove’

To use the autoremove command, you will first have to install it using pip.

pip install pip-autoremove

Running the below code will automatically delete or remove a package and also its unused dependencies.

pip-autoremove <package_name>

Following this article so far, we have covered almost all the methods that one can use to keep a mess-free virtual env and keep the same as clean as possible.

Best Practices to Maintain ‘virtual env’

Regularly Updating the Virtual env

Keeping track of what’s happening in the virtual env and always updating it would be extremely helpful to maintain a clean and mess-free environment.

Documenting Changes and Updates

Maintaining a record of all the changes happening is an excellent practice one can follow when working not only as a team but also as an individual. One can resolve things like ‘requirements.txt’ studied above to have an idea of the essentials of the projects.


As we have extensively covered all the methods and practices that an individual can follow to maintain a clean and tidy virtual env, following these practices will definitely present you as a better developer in others’ sight, and having a well-organized environment will keep you at peace while working on huge projects.

Resolving the practices mentioned above will actually help you in managing your projects well, as the project depends totally on the virtual env, and if your virtual env is sorted, you’re sure that the project is also sorted.


Official virtual env documentation.

How can I clean up virtual env.