# How to disable caching in .from\_pretrained()

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849%3E%5C
**Category:** 🤗Transformers\
**Created:** [September 1, 2024, 4:46pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849 "2024-09-01T16:46:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skumar1998](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/skumar1998/32/31137_2.png) [@skumar1998](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fskumar1998)%5C
**Post date:** [September 1, 2024, 4:46pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849%2F1 "2024-09-01T16:46:21Z")

</div>

I’m using multiple models inside my docker image and I don’t wanna cache it as it always be downloaded while starting the containers.  
Is there a way to ignore this or I have to write manual deletion logic?  
Thanks for any help and suggestions!

---

<div class="post-metadata">

**Author:** ![mikehemberger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mikehemberger/32/28013_2.png) [@mikehemberger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmikehemberger)%5C
**Post date:** [September 2, 2024, 6:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849%2F3 "2024-09-02T06:48:06Z")

</div>

Hey @skumar1998 ,  
You can set  
**force\_download** =True

 ![IMG_5659](https://us1.discourse-cdn.com/hellohellohello/original/3X/2/8/2827f909815a00ceb52d8942c2d2e346242c56f9.jpeg)  
To re-Download the model.  
Best,  
M

---

<div class="post-metadata">

**Author:** ![skumar1998](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/skumar1998/32/31137_2.png) [@skumar1998](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fskumar1998)%5C
**Post date:** [September 2, 2024, 5:28pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849%2F4 "2024-09-02T17:28:22Z")

</div>

Hi @mikehemberger  
Thanks for your quick reply. But it still stores model weights on cache memory.

---

<div class="post-metadata">

**Author:** ![BoltzmachineQ](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@BoltzmachineQ](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FBoltzmachineQ)%5C
**Post date:** [January 18, 2026, 10:06pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849%2F5 "2026-01-18T22:06:16Z")

</div>

This is indeed a very broken design….

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [January 18, 2026, 11:40pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-disable-caching-in-from-pretrained%2F104849%2F6 "2026-01-18T23:40:45Z")

</div>

I think bypassing the cache mechanism is practically impossible…

If you absolutely don’t want to keep the cache folder, and you have an environment where you can download and store model files locally beforehand, the best approach is to use `snapshot_download` with the `local_dir` option _and manually delete the `.cache` folder would be created within that local directory_.

* * *

You cannot make `from_pretrained()` download a model and _not_ write it to disk somewhere. The APIs are built around “download to local files, then load from local path.” That local-on-disk storage is the cache. `force_download=True` forces a fresh download, but it still lands in the cache directory by design. ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))

So the real choices are:

1. **Keep caching, but make it persistent** so containers stop re-downloading.
2. **Keep caching, but move it** so it is not the default shared `~/.cache/huggingface/...`.
3. **Deliberately use an ephemeral cache** (tmpfs or `/tmp`) and accept that you will re-download every start, or add deletion.
4. **Pre-bake the model files into the image** (or mount them) and run in offline/local-only mode.

* * *

## First, clear up the confusion: caching prevents re-downloads

If your container re-downloads on each start, it almost always means the cache directory is not persisted between runs. Common reasons:

- You run a new container each time and the cache lives inside the container filesystem.
- You run as a different user each time and the cache path changes.
- You set a cache directory under a path that is not mounted or is wiped.

Disabling caching would make this _worse_, because you would force downloads every start.

* * *

## Why `force_download=True` “still caches”

Transformers documents these knobs like this:

- `cache_dir=...` is where downloaded configs/weights get cached if you do not want the “standard cache.” ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))
- `force_download=True` means “re-download and override cached versions if they exist.” It does **not** mean “download without caching.” ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))

Separately, `huggingface_hub` file download functions are explicitly defined as “download a file if it’s not already present in the local cache.” ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/file_download "Downloading files"))  
That is the core design. Downloads go through the cache system.

Also, even when a file is cached, the Hub client may still do a small HTTP metadata check unless you enable hub offline mode. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))

* * *

## What you should do in Docker (recommended patterns)

### Pattern A: “Not shared” + no re-downloads (best for your case)

You said you do not want to use the shared default cache directory. Fine. Use a **dedicated cache directory that you control** , and **persist it** with a Docker volume.

Hugging Face supports:

- `HF_HOME` as the root for token + cache
- `HF_HUB_CACHE` as the specific Hub repo cache directory (models, datasets, spaces) ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))

**Docker Compose example**

```yaml
services:
  app:
    image: your-image
    volumes:
      - hf_cache:/hf-cache
    environment:
      HF_HUB_CACHE: /hf-cache/hub
volumes:
  hf_cache:

```

**Code stays normal**

```python
from transformers import AutoModel, AutoTokenizer

model_id = "bert-base-uncased"
tok = AutoTokenizer.from_pretrained(model_id)
mdl = AutoModel.from_pretrained(model_id)

```

Result:

- No default shared `~/.cache/huggingface/...`
- No re-download on restart
- No manual deletion logic

* * *

### Pattern B: Per-model isolation (still cached, but not shared even within your app)

If you want each model to have its own folder and never share artifacts:

```python
from transformers import AutoModel

mdl = AutoModel.from_pretrained(
    "bert-base-uncased",
    cache_dir="/models_cache/bert-base-uncased",
)

```

This uses caching, but the “cache” is your chosen directory, not the default. `cache_dir` is explicitly documented for this purpose. ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))

Tradeoff: you may duplicate files across folders.

* * *

### Pattern C: Bake models into the image and load locally (most deterministic runtime)

If you want containers to start with **zero downloads** and **zero dependency on a runtime cache** , download during the image build and load from a local directory.

Transformers’ offline documentation explicitly recommends pre-downloading a full model repo with `snapshot_download`. ([Hugging Face](https://huggingface.co/docs/transformers/en/installation "Installation"))

Then at runtime, do local-only loads:

- `local_files_only=True` (documented in the `from_pretrained` parameter list) ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))
- Optionally set `HF_HUB_OFFLINE=1` to prevent any Hub HTTP calls. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))

Runtime load example:

```python
from transformers import AutoModel

mdl = AutoModel.from_pretrained(
    "/models/bert-base-uncased",
    local_files_only=True,
)

```

This is the closest to “ignore caching” without re-downloading. You are not using the Hub cache as your runtime source. You are loading from a local folder.

* * *

## If you truly want “no cache left behind”

Be aware what you are asking for:

- If you delete weights after loading, you save disk space, but you will re-download next start.
- Transformers does not provide a “stream weights into RAM and never write to disk” mode.

If your real goal is “don’t persist anything on disk” for security or ephemeral workloads, do this:

1. Point cache to a temp location (`/tmp/hf`) via `HF_HUB_CACHE` or `cache_dir`.
2. Optionally run the container with a tmpfs mount so it is in-memory only.
3. Accept re-download cost, or add deletion at end.

This is operationally valid, but it is the opposite of “avoid downloads.”

* * *

## Preventing “it still phones home” (even when cached)

If you see network calls even when files exist locally, that can be the Hub metadata check. Hugging Face documents:

- `HF_HUB_OFFLINE=1` makes the hub do **no HTTP calls** and only use cached files. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))
- It also explicitly notes that without offline mode, `hf_hub_download` can still trigger an HTTP request even if the latest file is cached. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))

So for strict “never touch the network” runs:

- Set `HF_HUB_OFFLINE=1`
- Use `local_files_only=True` in `from_pretrained()` ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))

* * *

## Practical decision for your exact situation

You have multiple models in a Docker image and you complain they download every container start. That is a persistence problem, not a caching problem.

Do this:

1. **Stop using the default cache path** : set `HF_HUB_CACHE=/hf-cache/hub`. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))
2. **Persist it** with a Docker volume mount to `/hf-cache`.
3. If you also want zero network calls, add `HF_HUB_OFFLINE=1` once the cache is populated. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))

No manual deletion logic. No repeated downloads. No default shared cache directory.

* * *

## References (copyable)

```plaintext
HF hub env vars (HF_HOME, HF_HUB_CACHE, HF_HUB_OFFLINE):
https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables

Transformers from_pretrained parameters (cache_dir, force_download, local_files_only):
https://huggingface.co/docs/transformers/en/model_doc/auto

Transformers offline mode + snapshot_download recommendation:
https://huggingface.co/docs/transformers/en/installation

huggingface_hub file download semantics (“local cache”):
https://huggingface.co/docs/huggingface_hub/en/package_reference/file_download

```

* * *

### Summary

- `force_download=True` re-downloads but still writes to cache. That is expected. ([Hugging Face](https://huggingface.co/docs/transformers/en/model_doc/auto "Auto Classes"))
- There is no “download but don’t cache on disk” mode in `from_pretrained()`.
- Fix Docker re-downloads by persisting a **custom** cache dir via `HF_HUB_CACHE` and a volume. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))
- For strict offline, use `HF_HUB_OFFLINE=1` and `local_files_only=True`. ([Hugging Face](https://huggingface.co/docs/huggingface_hub/en/package_reference/environment_variables "Environment variables"))
