From cddacacb46dba3dab26dfaae65f82d4ac0737cde Mon Sep 17 00:00:00 2001 From: Mac Malone Date: Wed, 11 Feb 2026 18:33:09 -0500 Subject: [PATCH] feat: lake: `lake cache clean` (#12444) This PR adds the Lake CLI command `lake cache clean`, which deletes the Lake cache directory. --- src/lake/Lake/CLI/Help.lean | 14 +++++++++++++- src/lake/Lake/CLI/Main.lean | 15 +++++++++++++++ src/lake/Lake/Load/Workspace.lean | 3 ++- tests/lake/tests/cache/test.sh | 7 ++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/lake/Lake/CLI/Help.lean b/src/lake/Lake/CLI/Help.lean index ac8cc264f9..f43578d337 100644 --- a/src/lake/Lake/CLI/Help.lean +++ b/src/lake/Lake/CLI/Help.lean @@ -355,8 +355,9 @@ USAGE: lake cache COMMANDS: - get [] download artifacts into the Lake cache + get [] download artifacts into the local Lake cache put upload artifacts to a remote cache + clean removes ALL froms the local Lake cache See `lake cache help ` for more information on a specific command." @@ -448,6 +449,16 @@ derived from the package's current Git revision (and prefixed by the full scope). As such, the command will warn if the work tree currently has changes." +def helpCacheClean := +"Removes ALL files from the local Lake cache + +USAGE: + lake cache clean + +Deletes the configured Lake cache directory. If a workspace configuration +exists, this will delete the cache directory it uses. Otherwise, it will +delete the default Lake cache directory for the system." + def helpScriptCli := "Manage Lake scripts @@ -578,6 +589,7 @@ public def helpScript : (cmd : String) → String public def helpCache : (cmd : String) → String | "get" => helpCacheGet | "put" => helpCachePut +| "clean" => helpCacheClean | _ => helpCacheCli public def help : (cmd : String) → String diff --git a/src/lake/Lake/CLI/Main.lean b/src/lake/Lake/CLI/Main.lean index 6b7b5c00b1..db10c07b8e 100644 --- a/src/lake/Lake/CLI/Main.lean +++ b/src/lake/Lake/CLI/Main.lean @@ -537,6 +537,20 @@ protected def add : CliM PUnit := do let map ← CacheMap.load file ws.lakeCache.writeMap scope map opts.service? +protected def clean : CliM PUnit := do + processOptions lakeOption + let opts ← getThe LakeOptions + noArgsRem do + let cfg ← mkLoadConfig opts + let dir ← id do + if (← configFileExists cfg.configFile) then + return (← loadWorkspaceRoot cfg).lakeCache.dir + else if let some cache := cfg.lakeEnv.lakeCache? then + return cache.dir + else + error "no cache to delete; no workspace configuration found and no system cache detected" + IO.FS.removeDirAll dir + protected def help : CliM PUnit := do IO.println <| helpCache <| ← takeArgD "" @@ -546,6 +560,7 @@ def cacheCli : (cmd : String) → CliM PUnit | "add" => cache.add | "get" => cache.get | "put" => cache.put +| "clean" => cache.clean | "help" => cache.help | cmd => throw <| CliError.unknownCommand cmd diff --git a/src/lake/Lake/Load/Workspace.lean b/src/lake/Lake/Load/Workspace.lean index 8b8cc73843..3ee261edb1 100644 --- a/src/lake/Lake/Load/Workspace.lean +++ b/src/lake/Lake/Load/Workspace.lean @@ -23,10 +23,11 @@ open Lean namespace Lake /-- +**For internal use only.** Load a `Workspace` for a Lake package by elaborating its configuration file. Does not resolve dependencies. -/ -private def loadWorkspaceRoot (config : LoadConfig) : LogIO Workspace := do +public def loadWorkspaceRoot (config : LoadConfig) : LogIO Workspace := do Lean.searchPathRef.set config.lakeEnv.leanSearchPath let (root, env?) ← loadPackageCore "[root]" {config with pkgIdx := 0} let root := {root with outputsRef? := ← CacheRef.mk} diff --git a/tests/lake/tests/cache/test.sh b/tests/lake/tests/cache/test.sh index ba010accb9..e7da511892 100755 --- a/tests/lake/tests/cache/test.sh +++ b/tests/lake/tests/cache/test.sh @@ -180,8 +180,13 @@ test_lines 3 .lake/outputs.jsonl test_run build Test:static -o .lake/outputs.jsonl test_lines 6 .lake/outputs.jsonl -# Verify all artifacts end up in the cache directory with `restoreAllArtifacts` +# Verify that `lake cache clean` deletes the cache directory +test_exp -d "$CACHE_DIR" test_cmd cp -r "$CACHE_DIR" .lake/cache-backup +test_run cache clean +test_exp ! -d "$CACHE_DIR" + +# Verify all artifacts end up in the cache directory with `restoreAllArtifacts` test_cmd rm -rf "$CACHE_DIR" .lake/build test_run build -R -KrestoreAll=true \ test:exe Test:static Test:shared +Test:o.export +Test:o.noexport +Module