Gradle Error: Unable to delete file – solution for Kotlin

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The familiar pain of every Android developer

\n

You run Build → Clean Project in Android Studio, hoping for a quick project rebuild, but instead you see red text in the Build window:

\n
Error: Execution failed for task ':app:clean'.\n> Unable to delete file: C:\\Users\\User\\AndroidStudioProjects\\MyApp\\app\\build\\...\\some_file.dex
\n

Familiar? If you write in Kotlin for Android, this error can occur with alarming regularity, especially on Windows. It not only slows down development but also forces you to look for "magical" solutions like restarting your computer.

\n

In this article, we will break down why the Unable to delete file error occurs, how to fix it once and for all, and show code examples in Kotlin for automating the solution. By the end of the article, you will be able not only to fix the problem but also to prevent it.

\n

Let's start with the causes, then move on to solutions — from simple to advanced.

\n\n

Why can't Gradle delete the file?

\n

The Unable to delete file error when running the :app:clean task occurs when Gradle tries to clean the build/ folder but cannot delete one or more files. The main reasons:

\n
    \n
  • The file is locked by another process — most often this is an antivirus, Windows Search Indexer, or Android Studio itself.
  • \n
  • Long paths (Windows) — if the file path exceeds 260 characters, Windows may refuse to delete it.
  • \n
  • Unclosed input/output streams — your application or library may be holding the file open.
  • \n
  • Conflict with Instant Run / Apply Changes — older versions of Android Studio could lock files.
  • \n
\n

Most often the problem appears on Windows, but it can also occur on macOS/Linux when using SMB shares or Docker.

\n\n

Quick solutions (5 minutes)

\n

Before diving into the code, try these simple steps. They help in 80% of cases.

\n\n

1. Close Android Studio and delete manually

\n

The most reliable way is to close the IDE and manually delete the app/build folder. If the file does not delete, use Unlocker (Windows) or the lsof command (macOS/Linux) to find the blocking process.

\n\n

2. Use the terminal instead of the GUI

\n

Sometimes Android Studio "freezes" on the clean task. Run the command in the terminal:

\n
./gradlew clean --no-daemon
\n

The --no-daemon flag runs Gradle without a background process, which often solves the locking problem.

\n\n

3. Disable Instant Run / Apply Changes

\n

In Android Studio 3.x and older, Instant Run could lock files. Disable it:

\n
    \n
  • Go to File → Settings → Build, Execution, Deployment → Instant Run.
  • \n
  • Uncheck Enable Instant Run.
  • \n
\n

In newer versions (Arctic Fox+), this option is replaced by Apply Changes, but the problem occurs less frequently.

\n\n

Advanced solutions: automation in Kotlin

\n

If the quick methods didn't help, it's time to automate the process. We'll write a small script in Kotlin that will run before the clean task and forcibly close blocking processes.

\n\n

4. Using Kotlin DSL in Gradle for a custom task

\n

Add the following code to the build.gradle.kts file (module level):

\n
// build.gradle.kts (app level)\ntasks.register<Exec>("killFileLocks") {\n    description = "Closes processes blocking files in the build folder"\n    commandLine("cmd", "/c", "taskkill /f /im java.exe 2>nul || exit 0")\n    // For macOS/Linux replace with:\n    // commandLine("sh", "-c", "lsof -ti:8080 | xargs kill -9 2>/dev/null || true")\n}\n\ntasks.named("clean") {\n    dependsOn                

Blogs

Book Recommendations