SonarSource/sonarqube · error · IllegalStateException

Can not delete

Error message

Can not delete 

What it means

Files2.deleteIfExists(File) deletes a file or directory (recursively) if it exists, translating any IOException into an IllegalStateException with the message "Can not delete " plus the path. It is thrown when the underlying filesystem refuses or fails the deletion for any reason.

Source

Thrown at server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Files2.java:79

    }
    if (fileOrDir.isDirectory()) {
      FileUtils.deleteDirectory(fileOrDir);
    } else {
      Files.delete(fileOrDir.toPath());
    }
  }

  /**
   * Like {@link #deleteIfExistsOrThrowIOE(File)} but wraps {@link IOException}
   * into {@link IllegalStateException}.
   *
   * @throws IllegalStateException in case deletion is unsuccessful
   */
  public void deleteIfExists(File fileOrDir) {
    try {
      deleteIfExistsOrThrowIOE(fileOrDir);
    } catch (IOException e) {
      throw new IllegalStateException("Can not delete " + fileOrDir, e);
    }
  }

  /**
   * Deletes a directory or a file if it exists, else does nothing. In the case
   * of a directly, it is deleted recursively. Any exception is trapped and
   * ignored.
   *
   * @param fileOrDir file or directory to delete. Can be {@code null}.
   */
  public void deleteQuietly(@Nullable File fileOrDir) {
    FileUtils.deleteQuietly(fileOrDir);
  }

  /**
   * Moves a file.
   *
   * <p>

View on GitHub (pinned to 184c821202)

Solutions

  1. Check file/directory permissions and ownership on the target path and its parents
  2. Find and stop processes holding the file open (lsof/handle.exe), especially on Windows
  3. Verify the filesystem is writable and not a stale/read-only mount
  4. Exclude the SonarQube temp/work directories from antivirus and backup scans

Example fix

// before
files2.deleteIfExists(new File("/locked/dir")); // IllegalStateException
// after
File dir = new File("/locked/dir");
if (dir.exists() && dir.canWrite()) {
  files2.deleteIfExists(dir);
}
Defensive patterns

Strategy: validation

Validate before calling

File target = new File(path);
if (target.exists() && !target.canWrite()) {
  throw new IllegalStateException("No permission to delete " + target);
}

Try / catch

try {
  files2.deleteIfExists(fileOrDir);
} catch (IllegalStateException e) {
  LOGGER.warn("Deletion failed for {} : {}", fileOrDir, e.getCause());
  // fall back to deleteOnExit or alert an operator
}

Prevention

When it happens

Trigger: deleteIfExistsOrThrowIOE throws IOException — target file/directory locked by another process, insufficient permissions, directory not empty due to undeletable children, or path on read-only/unavailable filesystem.

Common situations: CE temp files still held open by a lingering process on Windows; temp directory owned by a different user after running the CE as another account; read-only mount or full NFS stall; antivirus locking files.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/f7f0506d4a49a7a3. Report an issue: GitHub.