Prune

Dev tools

Gradle cache taking up space: where it is and what to delete

Last checked Applies to Gradle and Gradle Wrapper builds, including Android Studio projects, on macOS and Windows 6 min read

Short answer

Yes, you can delete the Gradle cache. It sits in ~/.gradle/caches on Mac and C:\Users\<USERNAME>\.gradle\caches on Windows, and Gradle will download or rebuild whatever you remove on your next build. Stop the daemons with gradle --stop first, and only delete the caches folder. Don't delete the whole .gradle folder.

Path on Mac
~/.gradle/caches
Path on Windows
C:\Users\<USERNAME>\.gradle\caches
Moved it?
GRADLE_USER_HOME sets a different Gradle home on Mac and Windows
Safe to delete?
Yes, with the daemons stopped. You'll download dependencies again and the next build is slower.
Stop command
gradle --stop or ./gradlew --stop
Auto cleanup
Gradle prunes unused cache entries itself, by default once every 24 hours

What is inside the Gradle home

Gradle keeps everything shared between projects in one folder, the Gradle User Home. By default that's ~/.gradle on Mac and C:\Users\<USERNAME>\.gradle on Windows. If you've set the GRADLE_USER_HOME environment variable, that's the folder Gradle uses instead.

The folders that use disk space:

  • caches/modules-2 holds downloaded dependencies: JARs, POMs and metadata.
  • caches/build-cache-1 is the local build cache, task outputs Gradle can reuse.
  • caches/transforms-* and caches/jars-* hold files Gradle made from your dependencies.
  • caches/<version>/ folders hold compiled build scripts for one Gradle version each.
  • wrapper/dists holds full Gradle distributions the Wrapper downloaded, one per version your projects asked for.
  • jdks holds JDKs Gradle downloaded for toolchains.
  • daemon holds daemon logs.

The number at the end of a shared cache folder, like the 2 in modules-2, is the on-disk format version, so yours may show a different number.

Two things in this folder aren't cache: gradle.properties, your global Gradle settings, and init.d, your global init scripts. Keep both.

Gradle already cleans some of it

Many guides skip this. Gradle runs its own cleanup, by default once every 24 hours, in the background once the daemon stops. Per the Gradle docs it removes:

  • downloaded dependencies not used for 30 days
  • files Gradle created, and local build cache entries, not used for 7 days
  • version folders under caches/ for Gradle releases you haven't used for 30 days (7 days for snapshots)
  • Wrapper distributions in wrapper/dists that no version folder uses any more
  • daemon logs older than 14 days

So a big cache is often just what you've used in the last month. Deleting by hand mostly helps when you need the space now, or when you've stopped working on Gradle projects. If you've turned cleanup off, or a CI setup has, the cache keeps growing.

From Gradle 8.0 you can shorten these retention times with an init script in init.d. The docs show this example for ~/.gradle/init.d/cache-settings.init.gradle:

beforeSettings { settings ->
    settings.caches {
        downloadedResources.removeUnusedEntriesAfterDays = 45
        createdResources.removeUnusedEntriesAfterDays = 10
        buildCache.removeUnusedEntriesAfterDays = 5
    }
}

That example keeps downloads for 45 days, longer than the default. Set numbers below the defaults if you want a smaller cache.

Free the space in four steps

  1. Stop the Gradle daemons

    gradle --stop
    # or, inside a project that uses the Wrapper:
    ./gradlew --stop

    On Windows, it's .\gradlew --stop in PowerShell, from the project folder. It only stops daemons that the same Gradle version started, so you'll need to run it in each project that uses a different version. Or wait: an idle daemon stops on its own after 3 hours. Close Android Studio and other IDEs that run Gradle as well.

  2. Measure the cache

    du -sh ~/.gradle/caches/* ~/.gradle/wrapper/dists ~/.gradle/jdks
    # PowerShell
    Get-ChildItem "$env:USERPROFILE\.gradle\caches" -Directory | ForEach-Object {
      $gb = (Get-ChildItem $_.FullName -Recurse -File | Measure-Object Length -Sum).Sum / 1GB
      "{0,8:N2} GB  {1}" -f $gb, $_.Name
    }

    Start with whichever folder is biggest.

  3. Delete what you picked

    # the build cache only
    rm -rf ~/.gradle/caches/build-cache-1
    # or the whole cache folder
    rm -rf ~/.gradle/caches
    # PowerShell: the build cache only
    Remove-Item -Recurse -Force "$env:USERPROFILE\.gradle\caches\build-cache-1"
    # or the whole cache folder
    Remove-Item -Recurse -Force "$env:USERPROFILE\.gradle\caches"

    The build cache is the cheapest thing to lose, since Gradle just re-runs those tasks. If you delete all of caches, your next build will download every dependency again.

  4. Trim old Wrapper versions

    Look in wrapper/dists and delete Gradle versions no project of yours still uses. If a project does need one, the Wrapper downloads it again on the next build.

SafeDeleting caches/build-cache-1, old wrapper/dists versions or daemon logs. Gradle rebuilds or downloads them when a build needs them.

CarefulDeleting all of caches or jdks. Your next build needs the network and takes longer, and offline builds won't work until the downloads are back. Stop the daemons first so nothing is writing to the cache.

Do notdelete the whole .gradle folder. It also holds gradle.properties and init.d, your own settings and scripts, which Gradle can't download again.

If Gradle isn't the only developer cache on your disk, is the npm cache safe to delete covers the JavaScript side.

FAQ

Will deleting the Gradle cache break my Android Studio projects?

No. Your project files aren't in the cache. The next sync or build downloads the dependencies again, so it needs the network and takes longer once.

Why is my Gradle cache still big if Gradle cleans it automatically?

Gradle only removes entries that have gone unused for a while, 30 days for downloaded dependencies by default. Everything your projects used in that window stays.

Can I move the Gradle cache to another drive?

Yes. Set the GRADLE_USER_HOME environment variable to a folder on the other drive. It works the same on Mac and Windows. Move gradle.properties and init.d with it, because they live in the same folder.

Does gradle --stop stop every daemon?

No, it doesn't. It only stops daemons that the same Gradle version started. Run it in each project that uses a different version, or wait, since idle daemons stop after 3 hours.