Skip to content

Add Gradle cache cleanup using temporary project with gradlew --no-daemon #37

@markcallen

Description

@markcallen

Summary

The current gradle target in mac-cache-cleaner only reports the size of ~/.gradle/caches and ~/.gradle/wrapper/dists but has no cleanup commands. Unlike other tools (npm, pip, cargo), Gradle doesn't provide a native CLI command to clean its global cache.

Proposed Solution

Create a cleanup mechanism that:

  1. Creates a temporary minimal Gradle project with a wrapper
  2. Runs ./gradlew --no-daemon which performs cleanup when it completes
  3. Removes the temporary project directory

This approach leverages Gradle's built-in behavior where running with --no-daemon ensures cleanup happens when the JVM exits, avoiding orphaned daemon processes and their associated cache locks.

Implementation Details

  • Create a temp directory with minimal build.gradle.kts or build.gradle
  • Include settings.gradle.kts with project name
  • Run gradle wrapper to generate gradlew (or include pre-generated wrapper files)
  • Execute ./gradlew --no-daemon clean or a custom cleanup task
  • Clean up the temporary directory

Current State

{Name: "gradle", Enabled: true, Notes: "Gradle build caches and wrappers", 
 Paths: []string{"~/.gradle/caches", "~/.gradle/wrapper/dists"}, 
 Cmds: [][]string{},  // <-- No cleanup commands
 Tools: []Tool{{Name: "gradle", InstallCmd: "brew install gradle"}}},

Considerations

  • Requires Gradle or Java to be installed
  • Should handle cases where Gradle is not available gracefully
  • May need to stop running Gradle daemons first (gradle --stop)
  • Consider which cache directories are safe to clean:
    • ~/.gradle/caches/build-cache-* - Build cache (safe)
    • ~/.gradle/caches/transforms-* - Transform cache (safe)
    • ~/.gradle/caches/modules-* - Dependency cache (will re-download)
    • ~/.gradle/wrapper/dists/ - Downloaded Gradle distributions

Alternatives Considered

  1. Direct rm -rf ~/.gradle/caches/ - Works but violates "safety first" principle of using official CLI commands
  2. Gradle cache cleanup plugin - Requires modifying user's Gradle setup
  3. gradle --stop only - Frees memory but not disk space

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions