How to clear gradle cache?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The fastest way to clear the Gradle cache is to delete the ~/.gradle/caches/ directory. For the project-level build cache, run ./gradlew clean. To also stop the Gradle daemon (which holds file locks on cached artifacts), run ./gradlew --stop before deleting anything. Understanding which cache directory to clear, and why, saves you from unnecessary full-cache wipes that add minutes to your next build.
Gradle's Cache Structure
Gradle maintains several cache directories, each serving a different purpose. Clearing the wrong one either does nothing or forces a much longer rebuild than necessary.
| Directory | What it caches | Safe to delete? | Rebuild cost |
~/.gradle/caches/modules-2/ | Downloaded dependencies (JARs, POMs) | Yes | Re-downloads all dependencies |
~/.gradle/caches/transforms-3/ | Transformed artifacts | Yes | Re-runs artifact transforms |
~/.gradle/caches/build-cache-1/ | Task output cache | Yes | Re-executes previously cached tasks |
~/.gradle/wrapper/dists/ | Gradle distributions | Yes (rarely needed) | Re-downloads the Gradle wrapper |
<project>/.gradle/ | Project-level task history | Yes | Invalidates incremental builds |
<project>/build/ | Compiled classes, JARs, reports | Yes | Full recompile of the project |
Option 1: Delete the Global Cache Directory
This is the most thorough approach. It removes all downloaded dependencies, cached transforms, and build cache entries.
After this, the next build will re-download every dependency and re-execute every task. On a project with hundreds of dependencies, this can add several minutes.
Option 2: Clear Only the Dependency Cache
If your issue is a corrupted or stale dependency, target just the module cache.
This is the right choice when you see errors like "Could not resolve artifact" or when a dependency appears to be the wrong version despite your build file being correct.
Option 3: Clean the Project Build Directory
For project-level issues (stale compiled classes, outdated generated code), the clean task is sufficient.
This does not touch the global cache. Dependencies remain cached, and only the compiled output is regenerated.
Option 4: Invalidate the Build Cache
Gradle's build cache stores task outputs keyed by inputs. If you suspect the cache is returning incorrect results (rare, but possible after Gradle upgrades or plugin changes):
Note: cleanBuildCache was deprecated in Gradle 7 and removed in Gradle 8. Direct deletion of build-cache-1/ is now the recommended approach.
Option 5: Stop the Gradle Daemon
The Gradle daemon is a long-running process that caches project information in memory. If you are experiencing phantom build issues that persist after clearing disk caches, the daemon's in-memory state may be stale.
Stopping the daemon is also necessary before deleting cache directories on Windows, because the daemon holds file locks that prevent deletion.
Option 6: Refresh Dependencies Without Clearing Cache
If the issue is a SNAPSHOT dependency that has been updated upstream, you can force Gradle to re-check remote repositories without deleting the cache.
This is faster than a full cache clear because it only downloads dependencies whose checksums have changed since the last check.
Gradle Cache Configuration
You can control cache behavior in gradle.properties or settings.gradle:
Decision Flowchart
Choose the right cache-clearing strategy based on your symptom:
Common Pitfalls
- Clearing the entire
~/.gradle/directory instead of justcaches/. Thewrapper/dists/directory contains downloaded Gradle distributions. Deleting it forces a re-download of the Gradle wrapper, which is slow and unnecessary. Thegradle.propertiesfile in~/.gradle/contains your global configuration. Never delete the entire~/.gradle/directory blindly. - Forgetting to stop the daemon on Windows. The Gradle daemon locks files in the cache directory. Deleting files while the daemon is running produces partial deletions and "Access denied" errors. Always run
./gradlew --stopfirst. - Using
cleanBuildCacheon Gradle 8+. This task was removed. Use direct directory deletion or--no-build-cacheinstead. - Running
--refresh-dependencieson every build. This flag forces Gradle to contact remote repositories for every dependency on every build, which significantly slows down builds. Use it as a one-time fix, not a permanent workaround. - Not understanding incremental builds. Gradle tracks input/output hashes for each task. Clearing caches invalidates this tracking, forcing full recompilation. If your issue is limited to a single module, use
./gradlew :module:cleaninstead of a global cache wipe. - Clearing caches in CI/CD without a cache restore step. Many CI systems (GitHub Actions, GitLab CI) cache
~/.gradle/caches/between runs. If you clear it without updating the CI cache configuration, every subsequent pipeline run starts with a cold cache.
Summary
Gradle maintains global caches (dependencies in ~/.gradle/caches/modules-2/, build outputs in build-cache-1/) and project-level caches (.gradle/ and build/ directories). Match the cache-clearing approach to the problem: use --refresh-dependencies for stale SNAPSHOT versions, ./gradlew clean for stale build outputs, targeted deletion in modules-2/ for corrupted dependencies, and a full rm -rf ~/.gradle/caches/ only as a last resort. Always stop the Gradle daemon before deleting cache files. On CI systems, be mindful of pipeline cache configurations when clearing local caches.
Related reading
- How to clear or clean specific pod from the local cocoapods cache
- How to clear react-native cache?
- How to combine sharding and consistent hashing within a distributed system?
- How to configure Kafka to behave like a FiFo queue?
- How to clear the console using Java?
- How to clone an InputStream?
- How to configure RabbitMQ using Active/Passive High Availability architecture
- how to configure redis ttl with spring boot 2.0

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.