How to remove __pycache__ folders and .pyc files


These folders and files are generated by python. You should add these to your VCS’s ignore file. But if you need to clean these manually, you can do with the command below:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
This command will remove all files that end with .pyc
and __pycache__
folder recursively in the current directory. If you are using this command a lot, you should think about adding it to your shell alias list.
Note: For Python 3, you only need to remove __pycache__
folder because .pyc
files are inside them.
Alternative: You can prevent the generation of the file in the development using the environment variable below:
export PYTHONDONTWRITEBYTECODE=""
References
Subscribe to my newsletter
Read articles from Erdal TAŞKESEN directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Erdal TAŞKESEN
Erdal TAŞKESEN
Hey, y’all 🖖🏽 I am a passionate self-taught developer, open-source advocate, and tech enthusiast with a hands-on approach to problem-solving and an unending thirst for knowledge. Anything and everything classified as technology fascinate me.