1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use clap::Args;
use crate::storage::{gc, OciStore};
#[derive(Args, Debug)]
pub struct RmArgs {
/// Reference(s) to remove (e.g. registry.example.com/mymodel:latest)
#[arg(value_name = "REFERENCE", required = true, num_args = 1..)]
pub references: Vec<String>,
}
pub fn run(args: &RmArgs) -> anyhow::Result<()> {
let mut any_err = false;
// Resolve (and validate) every reference before opening the store, so an
// invalid ref never creates the store tree.
// resolve_ollama_api, not resolve: a bare name pulled via the Ollama API
// (POST /api/pull, /api/chat, ...) is stored under docker.io/ai/<name>,
// not hf.co/<name>, so it must resolve the same way here or `llmman rm
// <bare-name>` looks for the wrong entry.
let mut refs = Vec::new();
for raw in &args.references {
match crate::shortnames::resolve_ollama_api(raw) {
Ok(r) => refs.push(r),
Err(e) => {
eprintln!("Error removing {raw}: {e}");
any_err = true;
}
}
}
// Nothing valid to remove: don't open (and thereby create) the store.
if refs.is_empty() {
anyhow::bail!("one or more removals failed");
}
let store_root = crate::default_store()?;
let store = OciStore::open(&store_root)?;
let mut any_removed = false;
for reference in &refs {
match store.remove(reference) {
// The stored reference, not the one given: for a digest that
// is the tag it was held under.
Ok(removed) => {
println!("Removed {}", removed);
any_removed = true;
}
Err(e) => {
eprintln!("Error removing {}: {}", reference, e);
any_err = true;
}
}
}
// GC once, after every requested reference is untagged: recompute the
// still-referenced digest set from the surviving manifests, then sweep
// blobs/cache not in it. Uses the same grace window as serve's startup
// sweep, NOT zero: `llmman pull` runs in the long-lived `serve` daemon,
// which writes each layer blob to its final path as it downloads and
// only writes the manifest + tag at the very end. A concurrent
// `rm <unrelated>` would otherwise see that not-yet-tagged blob as an
// orphan and delete it mid-pull. The writer is a different process, so
// `rm` being synchronous doesn't protect it — only the grace window
// does (matches Ollama's layerPruneGracePeriod).
if any_removed && !gc::noprune_from_env() {
let (blob_stats, cache_stats) = gc::sweep(&store, &crate::default_cache()?)?;
if blob_stats.count > 0 || cache_stats.count > 0 {
println!(
"Freed {} ({} blobs, {} cache entries)",
crate::fmt::human_size(blob_stats.bytes + cache_stats.bytes),
blob_stats.count,
cache_stats.count
);
}
if blob_stats.kept_count > 0 {
println!(
"Kept {} ({} blobs) pulled less than an hour ago, freed by a later rm or serve restart",
crate::fmt::human_size(blob_stats.kept_bytes),
blob_stats.kept_count
);
}
}
if any_err {
anyhow::bail!("one or more removals failed");
}
Ok(())
}