feat: lake: lake cache clean (#12444)

This PR adds the Lake CLI command `lake cache clean`, which deletes the
Lake cache directory.
This commit is contained in:
Mac Malone 2026-02-11 18:33:09 -05:00 committed by GitHub
parent 204555ba83
commit cddacacb46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 3 deletions

View file

@ -355,8 +355,9 @@ USAGE:
lake cache <COMMAND>
COMMANDS:
get [<mappings>] download artifacts into the Lake cache
get [<mappings>] download artifacts into the local Lake cache
put <mappings> upload artifacts to a remote cache
clean removes ALL froms the local Lake cache
See `lake cache help <command>` 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

View file

@ -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

View file

@ -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}

View file

@ -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