Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Gradle clean or build cannot delete the build directory

ErrorUnable to delete directory 'C:\project\app\build'

Short answer

Gradle could not remove a folder because another process still holds a file in it. The error lists the stuck paths. The usual holders are a Gradle or Kotlin daemon, a file-sync tool such as Google Drive, or a process still writing into the folder. Stop the daemons first, then look at the listed paths.

  • Applies to Gradle 8.x, Windows 10 and 11, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read which files the message lists as failed, then ask Gradle which daemons are alive. A daemon that is still running after your build is the first suspect.

    .\gradlew.bat --status
    .\gradlew.bat --stop
  2. 2Match what you see, then jump to its fix

    1. 1Failed to delete some children, and the failed path is a .jar in build/libs or build/intermediatesA Gradle or Kotlin daemon still holds a jar in the folderFix
    2. 2the error returns after --stop, and the project sits inside a synced folder such as Google DriveA sync or backup tool is holding files in the project folderFix
    3. 3New files were found. This might happen because a process is still writing to the target directory.Something is still writing into the folderFix

What does the error look like?

  • Unable to delete directory 'C:\project\app\build'
  • Failed to delete some children. This might happen because a process has files open or has its working directory set in the target directory.
  • New files were found. This might happen because a process is still writing to the target directory.
  • The process cannot access the file because it is being used by another process

Cause 1 of 3

A Gradle or Kotlin daemon still holds a jar in the folder

1Check

List the running daemons. --status shows only daemons of the same Gradle version as the wrapper.

.\gradlew.bat --status

2Fix

Stop the daemons, then run the same task again. Gradle starts a fresh daemon and the old file handle is gone.

.\gradlew.bat --stop
.\gradlew.bat clean

3Verify

The clean task finishes and the build folder is gone.

dir app\build

Confirmed by docs.gradle.org

Did this fix it?

Cause 2 of 3

A sync or backup tool is holding files in the project folder

1Check

Check where the project lives. One Gradle issue on Windows 11 was traced to Google Drive for Desktop locking build folders; Gradle's maintainers closed it as an environment problem.

(Get-Location).Path

2Fix

Pause the sync client, or move the project to a folder it does not watch, then run the clean again.

# after moving or pausing sync
.\gradlew.bat --stop
.\gradlew.bat clean

3Verify

Run clean twice in a row. Both runs should finish without the error.

.\gradlew.bat clean
.\gradlew.bat clean

Confirmed by github.com

Did this fix it?

Cause 3 of 3

Something is still writing into the folder

1Check

Gradle prints the new files it found after its delete attempt. Read the file names to see which tool produces them.

.\gradlew.bat clean --stacktrace

2Fix

Stop the process that writes there, wait for it to end, then clean again. Gradle added the lists of failed paths and new files in 5.3 so you can find the writer.

.\gradlew.bat --stop
.\gradlew.bat clean

3Verify

The clean task ends with BUILD SUCCESSFUL.

.\gradlew.bat clean

Confirmed by docs.gradle.org

Did this fix it?

How do I stop it coming back?

  • Keep projects out of folders that a cloud-sync client watches, or exclude the build folders from sync.
  • Run gradlew --stop before you delete build folders by hand or switch Git branches.
  • Wait for running builds and tests to finish before you run clean.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android Studio cannot pair the phone over Wi-Fi

The Pair devices over Wi-Fi dialog lists no device

Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.

4 causes, each with a fix

Mobile apps

adb tcpip 5555 fails because no device is found

adb: more than one device/emulator

adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.