Gradle clean or build cannot delete the build directory
Unable 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.
Fix it fast
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 --stop2Match what you see, then jump to its fix
- 1
Failed 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
the 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
New files were found. This might happen because a process is still writing to the target directory.Something is still writing into the folderFix
- 1
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 --status2Fix
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 clean3Verify
The clean task finishes and the build folder is gone.
dir app\buildConfirmed by docs.gradle.org
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).Path2Fix
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 clean3Verify
Run clean twice in a row. Both runs should finish without the error.
.\gradlew.bat clean
.\gradlew.bat cleanConfirmed by github.com
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 --stacktrace2Fix
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 clean3Verify
The clean task ends with BUILD SUCCESSFUL.
.\gradlew.bat cleanConfirmed by docs.gradle.org
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
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile 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 fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile 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