How to Clean Python Cache Files

Updated · MintDisk team

MintDisk Developer Pack listing inactive projects, including a Python virtual environment

Python leaves several kinds of cache on a developer's Mac: __pycache__ folders next to your code, pip's download cache, virtual environments in old projects, and the package caches of conda and uv. Some are tiny and not worth thinking about; others reach many gigabytes. This guide explains each one, what Python cache cleanup actually gains, and the official commands to remove what you don't need.

Cleaning is optional. Python rebuilds or downloads everything again on demand, so clear caches when you need the space, not as a habit.

The commands work with the Python that ships with Apple's developer tools, with Python from python.org and with Homebrew's Python. Where a command deletes files, run it from the project folder you mean to clean.

__pycache__ folders

When Python imports a module, it saves the compiled bytecode in a __pycache__ folder next to the source file, as .pyc files such as module.cpython-312.pyc. Python checks the source's modification date against the compiled version and recompiles when it is out of date. This is completely automatic, and deleting the files is harmless: they are recreated on the next import.

They are also small. Removing them rarely frees much space, but it can be useful before you archive or copy a project. To list them in a project first:

find . -type d -name __pycache__ -prune
Terminal running find . -type d -name __pycache__ -prune, listing four __pycache__ folders in a project
Check the list first; the same command with -exec rm -rf {} + deletes them. Example output with demo files.

Then delete them, from the project's root folder:

find . -type d -name __pycache__ -prune -exec rm -rf {} +

Run this inside the project folder, never in your home folder or /. Check the current folder with pwd first: the command deletes permanently, without the Trash.

To stop Python writing these files, run it with -B or set PYTHONDONTWRITEBYTECODE=1. Since Python 3.8, PYTHONPYCACHEPREFIX moves all bytecode into one separate folder instead, which keeps your project folders clean.

Tool caches in your projects

Test runners, type checkers and linters keep their own cache folders inside each project, such as .pytest_cache, .mypy_cache and .ruff_cache. Like __pycache__, they are recreated on the next run and are normally listed in .gitignore. Deleting them is harmless, and the same find pattern shown above works for each name.

Machine-learning projects add another, much larger cache: models downloaded with Hugging Face libraries, stored in ~/.cache/huggingface/hub. The hf cache ls command lists them by size, and hf cache rm removes the ones you no longer need.

The pip cache

pip keeps downloaded packages and built wheels so it doesn't download them again. On a Mac the cache lives in ~/Library/Caches/pip by default. pip has its own commands to inspect and clear it:

python3 -m pip cache dir
python3 -m pip cache info
python3 -m pip cache purge
Terminal running python3 -m pip cache info, showing the cache locations, the 1.2 GB page cache and the built wheels
pip cache info shows where the cache is and how large it has grown. Example output with demo files.
  • pip cache dir prints the cache folder; pip cache info shows its size and contents.
  • pip cache remove <pattern> removes cached wheels of matching packages only.
  • pip cache purge removes everything: wheels and HTTP cache.

Clearing the pip cache is safe; the next install simply downloads again. pip's documentation advises against turning the cache off with --no-cache-dir unless something else already caches downloads.

Virtual environments

A virtual environment, often a .venv or venv folder in the project, contains its own copy of every package the project installed. For a data-science project that can be several gigabytes, and old projects often keep theirs long after you stopped working on them.

The Python documentation describes virtual environments as disposable: simple to delete and recreate from scratch, not meant to be moved or copied. Removing one is just deleting its folder.

  1. Make sure the project lists its dependencies, for example in requirements.txt, pyproject.toml or a lock file, so you can reinstall them later.
  2. Deactivate the environment if it is active (deactivate).
  3. Move the .venv folder to the Trash.
  4. To recreate it later: python3 -m venv .venv, activate it, and reinstall, for example with python3 -m pip install -r requirements.txt.

If a project has no requirements or lock file, write one before you delete its environment: python3 -m pip freeze > requirements.txt. Otherwise you may not know which versions it needed.

Find old virtual environments

Every virtual environment contains a pyvenv.cfg file at its top level, which makes them easy to find. This command lists them in your home folder, up to six levels deep, and only reads:

find ~ -maxdepth 6 -name pyvenv.cfg 2>/dev/null

To see their sizes, pass the folders to du:

find ~ -maxdepth 6 -name pyvenv.cfg -exec dirname {} ; 2>/dev/null | xargs -I{} du -sh {}

Environments in projects you have not opened for months are the best candidates. Check that each project lists its dependencies before you delete its environment.

conda and uv caches

conda

conda clean --all --dry-run
conda clean --all

conda clean --all removes the index cache, unused cached packages, tarballs, temporary files and log files. Run it with --dry-run first to see what would go. Avoid --force-pkgs-dirs: conda warns that it breaks environments whose packages are linked to the package cache.

uv

uv cache dir
uv cache prune
uv cache clean

uv cache prune removes unused cache entries and is the gentle option. uv cache clean removes everything in uv's cache. uv waits for other uv processes that use the cache, and its documentation warns that changing the cache folder by hand is never safe, so use these commands rather than deleting files.

What is worth cleaning

CacheTypical sizeSafe to removeCost
__pycache__SmallYesRecompiled on next import
pip cacheHundreds of MB to several GBYesPackages downloaded again
Old virtual environmentsUp to several GB eachYes, with a requirements fileReinstall when you return to the project
conda package cacheOften several GBYes, with conda cleanPackages downloaded again
uv cacheHundreds of MB to several GBYes, with uv cache prune or cleanPackages downloaded again

The biggest wins are usually old virtual environments and the conda cache. __pycache__ folders matter for tidiness, not for disk space.

Questions and answers

Does deleting __pycache__ make Python faster?

No. It slightly slows the next import of each module, because Python has to compile it again. Delete them for a clean copy of a project, not for speed.

Should __pycache__ be in git?

No. Add __pycache__/ to your .gitignore. Since Python 3.13, python3 -m venv also creates a .gitignore inside new virtual environments, so they stay out of git by default.

Can I delete ~/Library/Caches/pip directly?

Use python3 -m pip cache purge instead; it removes the same files and knows the cache's layout, including the location if you changed it.

How do I remove a conda environment?

Run conda env list to see your environments, then conda env remove -n name to delete one. As with virtual environments, export its packages first if you may need them again.

Why is a virtual environment so large?

Some packages ship big compiled libraries, for example machine-learning and data packages with native code. Each virtual environment gets its own copy, so two projects with the same heavy dependency use the space twice.

What is the difference between the pip cache and a virtual environment?

The pip cache holds downloaded packages, shared by every environment. A virtual environment holds the installed packages for one project. Clearing the cache never breaks an environment; deleting an environment means reinstalling into a new one.

Does Python clean its caches by itself?

Python replaces outdated .pyc files automatically, but it never deletes __pycache__ folders, pip's cache or old virtual environments. Those stay until you remove them.

Find old virtual environments across your projects

MintDisk's Developer Pack finds .venv and venv folders that contain a pyvenv.cfg, in projects you have not touched for 30 to 180 days, along with node_modules and other build folders, and shows the command that recreates each one. Selected folders go to the Trash, and your source code is never touched.

MintDisk Developer Pack listing inactive projects, their build folders and the command to rebuild each
MintDisk's Developer Pack: build folders of inactive projects, each with the command that rebuilds it.

Related

Sources

First published . Commands and menu paths were checked against the sources above on the update date.

Ask AI assistants about MintDisk

Independent, real-time insights from major language models.