Gradle
Dependencies
Dependency Management
Programming
Troubleshooting

How to force Gradle to redownload dependencies (--refresh-dependencies)

Codemia Knowledge Hub ·

Add the --refresh-dependencies flag to any Gradle command. It makes Gradle ignore cached resolution results and revalidate every resolved artifact against the remote repositories, downloading anything that changed.

bash
gradle build --refresh-dependencies

That covers most cases: corrupted downloads, stale SNAPSHOT versions, and resolution that stopped picking up new releases. For severe cache corruption, delete the cache directory.

How the Gradle dependency cache works

Gradle stores downloaded dependencies in ~/.gradle/caches/ (Linux/macOS) or C:\Users\<username>\.gradle\caches\ (Windows). The cache has two main parts:

  • 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.

During a build, Gradle checks the local cache first. If the artifact exists and the checksum matches, it skips the network. That makes repeat builds fast, and it is also why stale or corrupted artifacts can persist.

SNAPSHOT dependencies are cached for 24 hours by default. During that window, Gradle does not check the remote repository for updates unless forced.

Revalidating with --refresh-dependencies

bash
1# With Gradle wrapper (recommended)
2./gradlew build --refresh-dependencies
3
4# With system Gradle
5gradle build --refresh-dependencies

What the flag does:

  1. Checks the remote repository for each dependency.
  2. Downloads the artifact if the remote version is newer or the local copy's checksum does not match.
  3. Updates the local cache with fresh metadata.

It revalidates the cache rather than deleting it. Use it as a troubleshooting step, not a permanent flag: running it on every build defeats caching and slows builds significantly.

Deleting the whole Gradle cache

If --refresh-dependencies does not help, the cache itself may be corrupted. Delete it and let Gradle rebuild it. Stop the daemons first: deleting the cache while they run can cause file lock errors and leave the cache inconsistent.

bash
1# Stop all Gradle daemons first
2./gradlew --stop
3
4# Delete the dependency cache
5rm -rf ~/.gradle/caches/
6
7# Rebuild (downloads everything fresh)
8./gradlew build

On Windows:

powershell
1# Stop daemons
2.\gradlew.bat --stop
3
4# Delete cache
5Remove-Item -Recurse -Force "$env:USERPROFILE\.gradle\caches"
6
7# Rebuild
8.\gradlew.bat build

This affects every Gradle project on the machine; each one re-downloads all dependencies on its next build.

The dependency cache is not the same as the build cache (~/.gradle/caches/build-cache-1/), which stores task outputs for incremental builds. Deleting both works but is more disruptive than necessary.

Deleting a single dependency from the cache

If you know which dependency is corrupted, delete only that artifact:

bash
1# Delete a specific artifact (e.g., com.google.guava:guava)
2find ~/.gradle/caches -path "*/com.google.guava/guava*" -exec rm -rf {} +
3
4# Rebuild
5./gradlew build

This avoids the long rebuild for projects with hundreds of dependencies.

Disabling caching of changing modules in build.gradle

To make Gradle check remotes on every build, set the resolution strategy:

groovy
1// build.gradle
2configurations.all {
3    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
4    resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
5}

In Kotlin DSL:

kotlin
1// build.gradle.kts
2configurations.all {
3    resolutionStrategy {
4        cacheChangingModulesFor(0, "seconds")
5        cacheDynamicVersionsFor(0, "seconds")
6    }
7}
  • cacheChangingModulesFor controls how long SNAPSHOT dependencies are cached.
  • cacheDynamicVersionsFor controls how long dynamic versions (like 1.+ or latest.release) are cached.

Setting both to 0 seconds forces a remote check on every build. It helps during active development but slows builds, so do not leave it in production build scripts.

Shorter SNAPSHOT cache windows

When you depend on SNAPSHOTs from a CI artifact repository, the 24-hour default can be confusing. Set a shorter window:

groovy
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 10, 'minutes'
}

You can also mark specific dependencies as changing:

groovy
1dependencies {
2    implementation('com.example:my-lib:1.0-SNAPSHOT') {
3        changing = true
4    }
5}

cacheChangingModulesFor only applies to dependencies marked as changing. SNAPSHOT versions are marked automatically, but custom schemes (like 1.0-beta-latest) need changing = true.

Dynamic ranges like 1.+ make resolution unpredictable regardless of cache state. Pin versions in production builds.

Invalidating IntelliJ IDEA caches

IntelliJ IDEA has its own cache on top of Gradle's. If Gradle sees the right dependencies but the IDE still references old versions:

  1. Go to File > Invalidate Caches.
  2. Check Clear file system cache and Local History.
  3. Click Invalidate and Restart.
  4. After restart, reimport the Gradle project.

Comparing the options

MethodScopeDowntimeWhen to Use
--refresh-dependenciesCurrent buildMinimalStale SNAPSHOTs, missing updates
Delete ~/.gradle/caches/All projectsFull rebuildPersistent corruption, major Gradle upgrade
Delete specific artifactSingle artifactMinimalKnown corrupted dependency
cacheChangingModulesFor(0)SNAPSHOT/dynamicOngoing slowerActive development of SNAPSHOT deps
IntelliJ cache invalidationIDE indexIDE restartIDE sync mismatch

Checking which version resolved

bash
1# Show the full dependency tree
2./gradlew dependencies --configuration runtimeClasspath
3
4# Check a specific dependency
5./gradlew dependencyInsight --dependency guava --configuration runtimeClasspath

dependencyInsight shows which version was selected, why, and whether Gradle's conflict resolution settled any version conflicts.

The wrapper (./gradlew) and a system Gradle may be different versions with different cache formats, which confuses troubleshooting. Prefer the wrapper for consistency.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms