If you are wondering how to clear __pycache__ in a Python project, you can usually delete the folder safely. Python creates these directories to store compiled bytecode files, which can help programs start faster. The files are temporary: Python will recreate them when needed. You can remove one cache folder, clear caches throughout a project, or prevent bytecode files from being written in the first place. The right approach depends on your operating system and what you are trying to fix.
What Is the Python __pycache__ Folder?
When Python imports a module, it may compile the module’s source code into bytecode. Python stores this bytecode in a folder named __pycache__, usually beside the original .py file. The compiled files typically have names ending in .pyc, such as module.cpython-312.pyc.
The version details in a .pyc filename help Python distinguish bytecode produced by different Python versions. These files are an implementation detail—not your original source code—and they are normally safe to delete. If Python needs a cached file again, it can generate a replacement from the source file.
Clearing the cache does not uninstall Python, delete your program, or erase your installed packages. It removes generated bytecode files from the locations you choose. It also does not clear every kind of cache in a Python application; for example, a web framework or a library may manage separate caches of its own.
Why Clear __pycache__?
In everyday development, there is usually no need to clear Python’s bytecode cache. Python checks whether cached bytecode is current and generally updates it when the source changes. Still, removing cache files can be useful in a few situations:
- You are troubleshooting unexpected behavior. Clearing generated files can be a practical diagnostic step if you suspect a stale or unusual bytecode file.
- You want a clean project directory. Removing generated files can make a workspace easier to inspect or prepare for sharing.
- You are packaging or deploying code. Some workflows prefer to create deployment artifacts without local bytecode files.
- You are cleaning a repository. Bytecode should generally not be committed as part of a source-code project.
- You are changing Python environments. Removing caches can help tidy up after switching interpreters, although Python normally handles version-specific bytecode itself.
Deleting __pycache__ is not a substitute for fixing a bug in your source code. If a problem remains after the cache is cleared, investigate the import path, active Python interpreter, environment, and source files as well.
How to Clear __pycache__ on Linux or macOS
Open a terminal and move to your project directory. To remove a single cache folder, use rm -r with its path. For example, if the folder is inside src, run:
rm -r src/__pycache__
To delete all folders named __pycache__ beneath the current directory, use find:
find . -type d -name "__pycache__" -prune -exec rm -r {} +
This searches from the current directory, so run it from the root of the project you intend to clean. The -type d option limits the match to directories, and -name selects folders named __pycache__. The command removes those folders and their contents, including any .pyc files inside them.
If you prefer to remove only compiled .pyc files while leaving the cache directories in place, run:
find . -type f -name "*.pyc" -delete
Be cautious with recursive deletion commands. Check that your terminal is in the intended project folder before running them, and avoid changing the search path to a broad location such as your entire home directory unless that is specifically what you want.
How to Clear __pycache__ on Windows
In PowerShell, first navigate to the project directory. To remove one cache folder, provide its path:
Remove-Item -Recurse -Force .\src\__pycache__
To find and remove every __pycache__ folder below the current directory, use:
Get-ChildItem -Path . -Directory -Filter "__pycache__" -Recurse |
Remove-Item -Recurse -Force
As with the Linux and macOS command, start from your project’s root folder and verify the path first. PowerShell’s -Recurse option searches through subdirectories, while -Force helps remove hidden or read-only items where applicable.
You can also delete a cache folder using File Explorer. Open your project, find the folder named __pycache__, and delete it. For a large project, searching for __pycache__ within the project folder can help locate multiple copies. Make sure the search is limited to the project rather than your whole computer.
Clear Python Cache Folders with a Python Script
If you want a repeatable cleanup method, you can write a small Python script to remove every __pycache__ folder under a chosen directory. This example searches the current working directory:
from pathlib import Path
import shutil
for cache_dir in Path(".").rglob("__pycache__"):
if cache_dir.is_dir():
shutil.rmtree(cache_dir)
Run the script from the project root, or replace Path(".") with the path you want to clean. For instance, Path("/path/to/project") on Linux or macOS can target a specific directory. On Windows, use a raw string such as Path(r"C:\path\to\project").
For a safer first pass, print the directories that would be removed before deleting anything:
from pathlib import Path
for cache_dir in Path(".").rglob("__pycache__"):
if cache_dir.is_dir():
print(cache_dir)
Review the output before adding deletion logic. This is especially useful when adapting a cleanup script for a monorepo or a workspace that contains several unrelated projects.
Remove .pyc Files Without Deleting Cache Folders
Sometimes you may want to delete compiled bytecode files but leave the __pycache__ directories in place. In Python, you can locate and remove files with the .pyc extension:
from pathlib import Path
for pyc_file in Path(".").rglob("*.pyc"):
pyc_file.unlink()
This script removes matching files beneath the current directory. It does not remove other files in the cache folders. Use a project-specific starting path if you do not want the search to include every directory below your current location.
Prevent Python from Creating __pycache__
If you want to stop Python from writing bytecode files for a particular run, use the -B option:
python -B your_script.py
The equivalent environment variable is PYTHONDONTWRITEBYTECODE. Set it to a non-empty value before starting Python:
PYTHONDONTWRITEBYTECODE=1 python your_script.py
That environment-variable command works in common Linux and macOS shells. In PowerShell, set the variable for the current session and then run your script:
$env:PYTHONDONTWRITEBYTECODE = "1"
python your_script.py
Disabling bytecode writing can be useful in containers, temporary test runs, or environments where you want to keep a directory clean. It is not usually necessary for normal development, and it does not delete cache files that already exist. Remove those separately if you want to clear them.
Keep __pycache__ Out of Git
Python cache folders are generated files, so most source-code repositories should ignore them instead of tracking them. Add these patterns to the project’s .gitignore file:
__pycache__/
*.py[cod]
The first pattern ignores directories named __pycache__. The second ignores common Python bytecode file extensions, including .pyc, .pyo, and .pyd. If these files were already committed, adding a .gitignore rule alone will not remove them from Git’s tracking. You will need to remove the tracked files from the repository index and commit that change.
Ignoring cache files keeps code reviews focused on source changes and helps avoid committing interpreter-specific generated files. It does not prevent Python from creating them on your computer; it only tells Git not to include matching untracked files.
What to Know Before Clearing Python Cache
- Python may recreate the files. A later import or run can generate fresh bytecode, so deleted cache folders may return.
-
Your source files are separate. Removing
.pycfiles does not remove the corresponding.pyfiles. - Cache deletion may not fix the underlying issue. Check which interpreter and environment your application is using if behavior remains unexpected.
- Be precise with recursive commands. Use your project root as the starting location and review paths before deleting files.
-
Different tools may have different caches. Clearing
__pycache__does not automatically clear test, package-manager, browser, or framework caches.
Normally, you do not need to stop Python just to remove cache files. However, if a program is currently running, it may recreate files while it imports modules. For a tidy cleanup, stop the relevant development server or process, remove the cache folders, and then restart the program if needed.
Conclusion: Clear __pycache__ Safely
To clear __pycache__, delete the folder manually or use a recursive command from your project’s root directory. On Linux or macOS, find can locate and remove cache directories; on Windows, PowerShell offers similar recursive cleanup. You can also use a short Python script, remove only .pyc files, or prevent bytecode writing with -B or PYTHONDONTWRITEBYTECODE.
Cache files are generated and generally safe to remove, but Python may recreate them as your code runs. For most projects, the best long-term practice is to leave normal bytecode caching enabled and add __pycache__/ and *.py[cod] to .gitignore. That keeps your repository clean without interrupting Python’s usual behavior.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.