CLI Companion

  • Hugging Face CLI
    • login
    • whoami
    • repo create
    • upload
    • download
    • lfs-enable-largefiles
    • scan-cache
    • delete-cache
  • Hapi CLI
    • new
    • start
    • build
    • test
    • plugin create
    • route add
  • Cloudflared
    • tunnel
    • tunnel run
    • tunnel list
    • tunnel delete
    • access
    • access tcp
    • update

    The `huggingface-cli delete-cache` command is used to delete the Hugging Face cache, which stores downloaded models, datasets, and other files to speed up subsequent loads. This command helps manage disk space by removing cached data.

    Syntax

    bash
    huggingface-cli delete-cache [OPTIONS]

    Options

    * `-h`, `--help`: Show a help message and exit.

    * `--dir DIR`: Specify the cache directory to delete. If not specified, it deletes the default Hugging Face cache directory (typically `~/.cache/huggingface/hub`).

    * `--force`: Delete the cache directory without confirmation. Useful for scripting.

    * `--last-accessed LAST_ACCESSED`: Delete files not accessed since a specified duration or date. Examples: "1 week ago", "2023-01-01".

    * `--older-than OLDER_THAN`: Delete files older than a specified duration or date. Examples: "30 days", "2023-01-01".

    * `--size_lt SIZE_LT`: Delete files whose size is less than a specified size. Examples: "1GB", "500MB".

    * `--size_gt SIZE_GT`: Delete files whose size is greater than a specified size. Examples: "1GB", "500MB".

    Usage Examples

    1. **Delete the default Hugging Face cache (with confirmation prompt):**

    bash
    huggingface-cli delete-cache

    This command will prompt you for confirmation before deleting the cache located at `~/.cache/huggingface/hub` (or the directory specified by `HF_HOME`).

    2. **Delete the default Hugging Face cache without confirmation (forcefully):**

    bash
    huggingface-cli delete-cache --force

    This is useful for scripting or when you are certain about the deletion and want to bypass the interactive prompt.

    3. **Delete a specific custom cache directory:**

    bash
    huggingface-cli delete-cache --dir /path/to/my/custom/cache

    Use this if you've configured `HF_HOME` to a different location or are managing multiple cache directories.

    4. **Delete cache files that haven't been accessed in the last 30 days:**

    bash
    huggingface-cli delete-cache --last-accessed "30 days ago"

    This helps in cleaning up old, unused models and datasets while retaining recently used ones.

    5. **Delete cache files that are older than 6 months:**

    bash
    huggingface-cli delete-cache --older-than "6 months"

    Similar to `--last-accessed`, but based on the file's modification/creation time.

    6. **Delete cache files smaller than 100MB:**

    bash
    huggingface-cli delete-cache --size_lt "100MB"

    This can be useful for removing numerous small, potentially less important, cached files.

    Explanation

    The `huggingface-cli delete-cache` command is an essential tool for managing disk space used by the Hugging Face ecosystem. As you work with various models and datasets, the local cache can grow significantly. This command provides flexible options to prune the cache effectively.

    * **Default Behavior:** By default, without any specific options, the command targets the primary Hugging Face cache directory. It includes a confirmation step to prevent accidental data loss.

    * **Targeting Specific Caches:** The `--dir` option is vital for users who manage multiple projects or have configured custom cache locations, allowing them to precisely target which cache to clear.

    * **Automated Deletion:** The `--force` flag is indispensable for automation scripts or CI/CD pipelines where interactive prompts are not feasible.

    * **Intelligent Pruning:** Options like `--last-accessed`, `--older-than`, `--size_lt`, and `--size_gt` enable intelligent cache management. Instead of a wholesale deletion, you can specify criteria to remove only outdated, unused, or specific sized files. For example, regularly running `huggingface-cli delete-cache --last-accessed "6 months ago"` can free up substantial space by removing models that haven't been touched in a long time, while keeping frequently used assets intact.

    Regular use of `delete-cache` with these conditional options is a recommended practice for maintaining an organized development environment and optimizing disk usage.