Remove downloaded tensorflow and pytorchHugging face models
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TensorFlow, Keras, PyTorch, and Hugging Face libraries all cache downloaded model files so they do not need to fetch them again on every run. That is convenient for development, but the caches can grow quickly and consume several gigabytes of disk space if you experiment with many checkpoints.
Know Which Cache You Are Deleting
There is no single universal "machine learning models" folder. The exact location depends on the library and on environment variables you may have configured.
For Keras and many TensorFlow workflows, a common cache location is under the .keras directory in the home folder. For Hugging Face models used from either PyTorch or TensorFlow, the cache is commonly under .cache/huggingface unless you changed HF_HOME, TRANSFORMERS_CACHE, or a related variable.
Before deleting anything, inspect the directory so you know whether it contains models you still want.
Removing Keras Or TensorFlow Downloads
If you downloaded model weights through Keras utilities, clearing the local cache is often as simple as deleting the cached files.
That removes cached Keras model downloads but does not uninstall TensorFlow itself. The next time your code requests the same model, the library will download it again.
If you set a custom cache directory in your environment or in code, delete the files from that location instead of assuming the default path.
Removing Hugging Face Models Safely
Hugging Face caches can contain many snapshots, revisions, and shared blobs. You can remove them manually, but the library also provides tooling that helps you see what is taking space.
That command reports cached repositories and their sizes. If you want to clear everything:
If you prefer to remove only one repository, inspect the cache report first and delete the specific cached repo directory rather than wiping the entire cache blindly.
Find The Active Cache Location From Python
If you are unsure which directory your program is using, inspect the relevant environment variables from the same environment that runs the model code.
For Keras, you can inspect the home directory and any custom configuration your application applies.
This avoids deleting the wrong directory on shared machines or inside virtual environments.
Clearing A Specific Downloaded Model In Code
Sometimes the simplest workflow is to find the actual path that was used by your code and delete only that model directory.
That kind of inspection is safer than a blanket delete when you have multiple large models cached.
Containers And Notebooks
In notebooks, Docker images, or remote servers, caches may live in different places from your local shell account. For example, a container might write under /root/.cache/huggingface, not your normal home directory. If a delete command seems to do nothing, verify you are cleaning the same filesystem and user account that created the cache.
What Happens After Deletion
Deleting cached model files only removes local copies. It does not change the model on Hugging Face Hub or any remote registry, and it does not break your code permanently. It simply means the next load will re-download the assets if the program still refers to them.
Common Pitfalls
A common mistake is deleting the Python package and expecting that to clear cached models; installed libraries and cached model weights are separate concerns. Another is wiping a cache from one shell while the notebook or service that uses the models runs under another user or container. Developers also sometimes remove shared cache blobs while a process is actively reading them, which can cause confusing runtime errors. Stop the process first if you are doing large cache cleanup on a live system.
Summary
- TensorFlow or Keras downloads and Hugging Face downloads usually live in different cache directories.
- Check the active cache path before deleting files, especially on shared or containerized systems.
- '
huggingface-cli scan-cacheis useful for inspecting Hugging Face cache usage.' - Deleting cached models only removes local copies; future runs can download them again.
- Be careful not to confuse package uninstall steps with model-cache cleanup.
Related reading
- Removing then Inserting a New Middle Layer in a Keras Model
- Removing then Inserting a New Middle Layer in a Keras Model
- Rename variable scope of saved model in TensorFlow
- Replace nan values in tensorflow tensor
- return_sequences False equivalent in pytorch LSTM
- Run Identical model on multiple GPUs, but send different user data to each GPU
- Replace Validation Monitors with tf.train.SessionRunHook when using Estimators
- Replacing placeholder for tensorflow v2
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.