void (CompileContext context, final @Nullable Set<File> dirsToDelete) { if (dirsToDelete == null || dirsToDelete.isEmpty()) { return; } Set<File> doNotDelete = ALL_OUTPUTS_KEY.get(context); if (doNotDelete == null) { doNotDelete = FileCollectionFactory.createCanonicalFileSet(); for (BuildTarget<?> target : context.getProjectDescriptor().getBuildTargetIndex().getAllTargets()) { doNotDelete.addAll(target.getOutputRoots(context)); } ALL_OUTPUTS_KEY.set(context, doNotDelete); } Set<File> additionalDirs = null; Set<File> toDelete = dirsToDelete; while (toDelete != null) { for (File file : toDelete) { // important: do not force deletion if the directory is not empty! final boolean deleted = !doNotDelete.contains(file) && file.delete(); if (deleted) { final File parentFile = file.getParentFile(); if (parentFile != null) { if (additionalDirs == null) { additionalDirs = FileCollectionFactory.createCanonicalFileSet(); } additionalDirs.add(parentFile); } } } toDelete = additionalDirs; additionalDirs = null; } }