How can I force Gradle to redownload dependencies?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The fastest way to force Gradle to redownload dependencies is the --refresh-dependencies flag. Append it to any Gradle command to bypass the local cache and fetch fresh copies of all resolved artifacts from remote repositories.
This handles the majority of cases: corrupted downloads, stale SNAPSHOT versions, and dependency resolution that stopped picking up new releases. For more severe cache corruption, you may need to delete the cache directory entirely.
How Gradle Caching Works
Gradle stores downloaded dependencies in a local cache at ~/.gradle/caches/ (Linux/macOS) or C:\Users\<username>\.gradle\caches\ (Windows). The cache has two main sections:
- Module cache (
modules-2/): Stores downloaded JARs, POMs, and metadata indexed by group, artifact, and version. - File locks and hashes: Gradle tracks checksums to verify artifact integrity.
When you run a build, Gradle checks the local cache first. If the artifact exists and the checksum matches, it skips the network request entirely. This is why builds are fast after the first run but also why stale or corrupted artifacts can persist.
For SNAPSHOT dependencies, Gradle caches them for 24 hours by default. During that window, it will not check the remote repository for updates unless forced.
Method 1: --refresh-dependencies
This flag tells Gradle to revalidate all cached dependencies against the remote repositories:
What this actually does:
- Checks the remote repository for each dependency.
- Downloads the artifact if the remote version is newer or the local copy's checksum does not match.
- Updates the local cache with fresh metadata.
This does not delete the cache. It revalidates it. For most "stale dependency" issues, this is the correct fix.
Method 2: Delete the Gradle Cache
When --refresh-dependencies does not solve the problem, the cache itself may be corrupted. Delete it and let Gradle rebuild from scratch.
On Windows:
This affects all Gradle projects on the machine, not just the current one. Every project's next build will re-download all dependencies.
Method 3: Delete a Specific Dependency
If you know exactly which dependency is corrupted, you can delete only that artifact from the cache instead of wiping everything:
This is more surgical than deleting the entire cache and avoids the long rebuild time for projects with hundreds of dependencies.
Method 4: Force Resolution Strategy in build.gradle
For programmatic control, you can configure Gradle's resolution strategy to force specific dependencies to refresh on every build:
In Kotlin DSL:
cacheChangingModulesForcontrols how long SNAPSHOT dependencies are cached.cacheDynamicVersionsForcontrols how long dynamic versions (like1.+orlatest.release) are cached.
Setting both to 0 seconds forces Gradle to check the remote repository on every build. This is useful during active development but slows down builds, so do not leave it in production build scripts permanently.
Method 5: Invalidate Caches in IntelliJ IDEA
If you are using IntelliJ IDEA and Gradle sync is stuck on old dependencies, the IDE has its own cache layer on top of Gradle's:
- Go to File > Invalidate Caches.
- Check Clear file system cache and Local History.
- Click Invalidate and Restart.
- After restart, reimport the Gradle project.
This resolves cases where Gradle itself sees the correct dependencies but IntelliJ's index still references old versions.
Method Comparison
| Method | Scope | Downtime | When to Use |
--refresh-dependencies | Current build | Minimal | Stale SNAPSHOTs, missing updates |
Delete ~/.gradle/caches/ | All projects | Full rebuild | Persistent corruption, major Gradle upgrade |
| Delete specific artifact | Single artifact | Minimal | Known corrupted dependency |
cacheChangingModulesFor(0) | SNAPSHOT/dynamic | Ongoing slower | Active development of SNAPSHOT deps |
| IntelliJ cache invalidation | IDE index | IDE restart | IDE sync mismatch |
Configuring SNAPSHOT Cache Duration
For projects that depend on SNAPSHOT versions from a CI artifact repository, the default 24-hour cache can cause confusion. Configure a shorter window:
You can also mark specific dependencies as "changing" explicitly:
Gradle only applies cacheChangingModulesFor to dependencies marked as changing. SNAPSHOT versions are marked automatically, but custom versioning schemes (like 1.0-beta-latest) need the explicit changing = true.
Verifying Dependencies After Refresh
After forcing a redownload, verify that the correct version resolved:
The dependencyInsight task shows which version was selected, why, and whether any version conflicts were resolved by Gradle's conflict resolution.
Common Pitfalls
Running --refresh-dependencies on every build defeats the purpose of caching and significantly slows down build times. Use it as a troubleshooting step, not a permanent flag.
Deleting ~/.gradle/caches/ while Gradle daemons are still running can cause file lock errors and leave the cache in an inconsistent state. Always run ./gradlew --stop first.
Confusing Gradle's dependency cache with the build cache (~/.gradle/caches/build-cache-1/) leads to deleting the wrong directory. The build cache stores task outputs for incremental builds. The dependency cache stores downloaded JARs. Deleting both works but is more disruptive than necessary.
Using dynamic version ranges like 1.+ in production builds makes dependency resolution unpredictable regardless of cache state. Pin versions explicitly and use --refresh-dependencies only for troubleshooting.
Forgetting that Gradle wrapper (./gradlew) and system Gradle may use different Gradle versions with different cache formats can cause confusion when troubleshooting. Always prefer the wrapper for consistency.
Summary
- Use
./gradlew build --refresh-dependenciesas the first troubleshooting step for stale dependencies. - Delete
~/.gradle/caches/only when cache corruption is persistent, and always stop daemons first. - Use
cacheChangingModulesFor(0, "seconds")inbuild.gradleto force fresh SNAPSHOT resolution during active development. - Verify resolved versions with
./gradlew dependenciesordependencyInsight. - Never leave aggressive cache-busting settings in production build scripts.
- In IntelliJ IDEA, invalidate IDE caches separately if Gradle sync does not reflect updated dependencies.

