Delete a cached model from disk. For HuggingFace cache models, removes the entire `models--org--name/` directory (including all snapshots and blobs). For cluster cache models, removes the model directory. Refuses to delete local models.
(model: &LocalModel)
| 128 | /// (including all snapshots and blobs). For cluster cache models, removes the model |
| 129 | /// directory. Refuses to delete local models. |
| 130 | pub fn delete_model(model: &LocalModel) -> Result<()> { |
| 131 | match model.source { |
| 132 | ModelSource::HuggingFaceCache => { |
| 133 | // model.path is .../snapshots/{hash}/ — go up to models--org--name/ |
| 134 | let hf_model_dir = model |
| 135 | .path |
| 136 | .parent() // snapshots/ |
| 137 | .and_then(|p| p.parent()) // models--org--name/ |
| 138 | .ok_or_else(|| anyhow::anyhow!("unexpected HF cache structure for {}", model.path.display()))?; |
| 139 | std::fs::remove_dir_all(hf_model_dir)?; |
| 140 | } |
| 141 | ModelSource::ClusterCache { .. } => { |
| 142 | std::fs::remove_dir_all(&model.path)?; |
| 143 | } |
| 144 | ModelSource::Local => { |
| 145 | anyhow::bail!( |
| 146 | "refusing to delete local model '{}' — use rm -rf manually", |
| 147 | model.name |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | Ok(()) |
| 152 | } |
| 153 | |
| 154 | /// Check a single directory and return its model status, or None if it's not a model dir. |
| 155 | fn check_model_dir(dir: &Path) -> Option<(ModelStatus, u64)> { |