Open In App

Different Ways to fix “Execution failed for task ‘:app:clean’. Unable to delete file” error in Android Studio

Last Updated : 17 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

When you try to rebuild or clean and build your project (containing mostly Kotlin code) in Android Studio, it fails and throws the error: 

Execution failed for task ‘:app:clean’.

Unable to delete file: SpecificFileLocation

So, In this article, we are going to see the topics: why there is a need to solve this error and how to solve this error. We will discuss six different methods for fixing the “Execution failed for task ‘:app:clean’. Unable to delete file” error.

Why There is a Need To Fix “Execution failed for task ‘:app:clean’. Unable to delete file”?

When you do some editing to generated files and try to rebuild the application, the rebuild fails due to some files being locked. This happens on a Linux system when using a project located on an NTFS filesystem. The trace indicates when a file (which is still open) was opened. A file that opens on an NTFS partition is always locked up. The Java process puts a lock on the build files when the project that contains Kotlin files is built, and the build files cannot be deleted during the app:clean task. It continues to fail, even if all the Kotlin files are removed, The lock remains until Java is killed. You may face this error when you try to change the package structure of the project. You accidentally rename or move original(source) folders, rather than refactoring through Android Studio. Now Because of the error  “Execution failed for task ‘:app:clean’. Unable to delete file” you are unable to rebuild or clean the project. Rebooting your computer or doing something else will not help. So we need to solve this error.

How to solve “Execution failed for task ‘:app:clean’. Unable to delete file”?

Method 1

  • Close Android Studio. Then try to manually delete the build files and open Android Studio again.
  • We need to kill the java.exe process while Android Studio is running. To do so Open the command prompt and type TASKKILL /F /IM java.exe. This will kill all JAVA TM processes automatically. Now re-compile the project again, it should work.

Method 2

Navigate to File > Settings > Build, Execution, Deployment > Instant Run > Uncheck this Check box (Enable Instant Run to hot swap code)

Method 3

In Android Studio > File > Invalidate Caches / Restart.

Method 4

Try to clean the project by using the command gradlew clean from Terminal and hit Enter button. When you reopen the project you will need to run Gradle sync again.

Method 5

Delete the directory intermediates. It is a quick solution to this error. The directory will be rebuild when the project is rebuilt. The “intermediates” folder has individual files that are created during the build process and which are eventually combined to produce the “apk” file.  

Method 6

Download LockHunter (at your own risk ), then add the following code to your module’s gradle.build file, by replacing the lockhunter variable with your path to LockHunter.exe. This will cause LockHunter to forcefully and silently unlock and delete the build files when the app:clean task runs

task clean(type: Exec) {

   ext.lockhunter = ‘\”C:\\LockHunter.exe\”‘

   def buildDir = file(new File(“build”))

   commandLine ‘cmd’, “$lockhunter”, ‘/delete’, ‘/silent’, buildDir

}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads