tensorflow
pytorch
hugging face
remove models
downloaded models

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.

Practice ML system design

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.

bash
ls ~/.keras
ls ~/.cache/huggingface

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.

bash
rm -rf ~/.keras/models/*

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.

bash
huggingface-cli scan-cache

That command reports cached repositories and their sizes. If you want to clear everything:

bash
rm -rf ~/.cache/huggingface/hub/*

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.

python
1import os
2
3for name in ["HF_HOME", "TRANSFORMERS_CACHE", "XDG_CACHE_HOME"]:
4    print(name, os.environ.get(name))

For Keras, you can inspect the home directory and any custom configuration your application applies.

python
from pathlib import Path

print(Path.home() / ".keras")

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.

python
1from pathlib import Path
2
3cache_dir = Path.home() / ".cache" / "huggingface" / "hub"
4for path in cache_dir.iterdir():
5    if "bert-base-uncased" in path.name:
6        print("Delete:", path)

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-cache is 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.