public static void deleteDirectory(File directory) throws IOException {
        if (directory != null && directory.exists()) {
            try (Stream<Path> files = Files.walk(directory.toPath())) {
                files.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
            }
            directory.delete();
            if (directory.exists()) {
                throw new IOException("Unable to delete directory " + directory);
            }
        }
    }
A tool class for safely creating and deleting directories that wraps exception information.
inlong-sort/sort-flink/sort-flink-v1.13/sort-connectors/iceberg/src/main/java/org/apache/inlong/sort/iceberg/sink/collections/FileIOUtils.java