gradle
cache clearing
gradle cache
build tools
software development

How to clear the Gradle cache

Codemia Knowledge Hub ·

To clear the Gradle cache, stop the daemon with ./gradlew --stop (it holds file locks on cached artifacts), then delete ~/.gradle/caches/. For project build output, run ./gradlew clean. Picking the right cache to clear avoids full wipes that add minutes to your next build.

Where Gradle keeps its caches

Gradle has several cache directories with different jobs. Clearing the wrong one either does nothing or forces a much longer rebuild than needed.

text
1~/.gradle/
2  caches/
3    modules-2/          # Downloaded dependency JARs and POMs
4    transforms-3/       # Artifact transforms (e.g., desugaring, resource merging)
5    build-cache-1/      # Task output cache (shared across projects)
6    jars-9/             # Cached classpath analysis
7    journal-1/          # File system access journal
8  wrapper/
9    dists/              # Downloaded Gradle distributions (not usually worth clearing)
10  daemon/
11    <version>/          # Daemon logs and registry
12
13<project>/
14  .gradle/              # Project-specific caches (task history, file hashes)
15  build/                # Build outputs (classes, JARs, reports)
DirectoryWhat it cachesSafe to delete?Rebuild cost
~/.gradle/caches/modules-2/Downloaded dependencies (JARs, POMs)YesRe-downloads all dependencies
~/.gradle/caches/transforms-3/Transformed artifactsYesRe-runs artifact transforms
~/.gradle/caches/build-cache-1/Task output cacheYesRe-executes previously cached tasks
~/.gradle/wrapper/dists/Gradle distributionsYes (rarely needed)Re-downloads the Gradle wrapper
<project>/.gradle/Project-level task historyYesInvalidates incremental builds
<project>/build/Compiled classes, JARs, reportsYesFull recompile of the project

Delete the whole global cache

The most thorough option removes all downloaded dependencies, cached transforms and build cache entries:

bash
1# Stop the daemon first to release file locks
2./gradlew --stop
3
4# Delete the global cache
5rm -rf ~/.gradle/caches/
6
7# On Windows (PowerShell)
8./gradlew --stop
9Remove-Item -Recurse -Force "$env:USERPROFILE\.gradle\caches"

The next build re-downloads every dependency and re-runs every task, which can add several minutes on a project with hundreds of dependencies.

Clear only the dependency cache

For a corrupted or stale dependency, target the module cache:

bash
1# Remove only downloaded dependencies
2rm -rf ~/.gradle/caches/modules-2/
3
4# Or remove a specific group's artifacts
5rm -rf ~/.gradle/caches/modules-2/files-2.1/com.google.guava/
6
7# On Windows
8Remove-Item -Recurse -Force "$env:USERPROFILE\.gradle\caches\modules-2\files-2.1\com.google.guava"

Use this for errors like "Could not resolve artifact" or when a dependency resolves to the wrong version even though the build file is correct.

Clean the project build directory

For stale compiled classes or outdated generated code, clean is enough:

bash
1# Deletes the build/ directory
2./gradlew clean
3
4# Clean and rebuild
5./gradlew clean build
6
7# Clean a specific subproject in a multi-module build
8./gradlew :app:clean
9./gradlew :library:clean

This leaves the global cache alone. Dependencies stay cached and only compiled output is regenerated.

Invalidate the build cache

The build cache stores task outputs keyed by inputs. If you suspect it is returning wrong results (rare, but possible after Gradle upgrades or plugin changes):

bash
1# Delete the local build cache
2rm -rf ~/.gradle/caches/build-cache-1/
3
4# Or disable the build cache for a single run
5./gradlew build --no-build-cache
6
7# Or run a cleanBuildCache task, if a plugin or your build defines one
8./gradlew cleanBuildCache

cleanBuildCache is not a built-in task in current Gradle versions, so the command only works if a plugin or your build defines it. Deleting build-cache-1/ directly works everywhere.

Stop the Gradle daemon

The daemon is a long-running process that keeps project information in memory. If phantom build issues survive clearing disk caches, its in-memory state may be stale:

bash
1# Stop all running daemons
2./gradlew --stop
3
4# Verify no daemons are running
5./gradlew --status
6
7# Force a build without the daemon (useful for debugging)
8./gradlew build --no-daemon

On Windows you must also stop the daemon before deleting cache directories, because it holds file locks that block deletion.

Refresh dependencies without deleting the cache

If a SNAPSHOT dependency changed upstream, force Gradle to re-check remote repositories:

bash
1# Force refresh of all dependencies
2./gradlew build --refresh-dependencies
3
4# This re-downloads changed artifacts but keeps unchanged ones cached

This is faster than a full clear because only dependencies whose checksums changed are downloaded.

Configuring cache behavior

Control caching in gradle.properties or settings.gradle:

properties
1# gradle.properties
2
3# Disable the build cache entirely
4org.gradle.caching=false
5
6# Control dependency caching TTL for dynamic versions
7# (e.g., how long to cache SNAPSHOT lookups)
8# In build.gradle:

A custom build cache directory is set in settings.gradle, not in gradle.properties:

groovy
1// settings.gradle
2buildCache {
3    local {
4        directory = new File(rootDir, 'build-cache')
5    }
6}
groovy
1// build.gradle - configure cache expiration for changing dependencies
2configurations.all {
3    resolutionStrategy {
4        // Re-check SNAPSHOT dependencies every build
5        cacheChangingModulesFor 0, 'seconds'
6
7        // Re-check dynamic versions (e.g., 1.0.+) every 24 hours
8        cacheDynamicVersionsFor 24, 'hours'
9    }
10}

Which cache to clear for each symptom

text
1Build fails with "Could not resolve dependency"
2  -> rm -rf ~/.gradle/caches/modules-2/
3  -> ./gradlew build --refresh-dependencies
4
5Build uses wrong version of a SNAPSHOT
6  -> ./gradlew build --refresh-dependencies
7
8Stale compiled classes or generated code
9  -> ./gradlew clean build
10
11Build cache returns wrong task outputs
12  -> rm -rf ~/.gradle/caches/build-cache-1/
13  -> ./gradlew build --no-build-cache
14
15Everything is broken, nothing makes sense
16  -> ./gradlew --stop
17  -> rm -rf ~/.gradle/caches/
18  -> rm -rf <project>/.gradle/
19  -> ./gradlew clean build

Mistakes when clearing the Gradle cache

  • Deleting all of ~/.gradle/ instead of caches/. wrapper/dists/ holds downloaded Gradle distributions, and re-downloading them is slow and unnecessary. ~/.gradle/gradle.properties holds your global configuration.
  • Not stopping the daemon on Windows. Its file locks cause partial deletions and "Access denied" errors. Run ./gradlew --stop first.
  • Relying on a cleanBuildCache task. Gradle does not ship one; delete the directory or use --no-build-cache.
  • Running --refresh-dependencies on every build. It contacts remote repositories for every dependency each time and slows builds significantly. Treat it as a one-time fix.
  • Wiping global caches for a single-module problem. Clearing caches invalidates Gradle's input/output hash tracking and forces full recompilation. Use ./gradlew :module:clean instead.
  • Clearing caches in CI without updating the cache restore step. Many CI systems (GitHub Actions, GitLab CI) cache ~/.gradle/caches/ between runs; clear it without updating that configuration and every later run starts cold.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design