public static void deleteFilesRecursively ( File ... files ) throws IOException { if ( files == null ) return ; List < File > filesToDelete = new ArrayList <> ( ) ; filesToDelete . addAll ( Arrays . asList ( files ) ) ; while ( ! filesToDelete . isEmpty ( ) ) { File currentFile = filesToDelete . remove ( 0 ) ; if ( currentFile == null || ! currentFile . exists ( ) ) continue ; // Non-empty directory: add sub-files and reinsert the current directory after File [ ] subFiles = currentFile . listFiles ( ) ; if ( subFiles != null && subFiles . length > 0 ) { filesToDelete . add ( 0 , currentFile ) ; filesToDelete . addAll ( 0 , Arrays . asList ( subFiles ) ) ; } // Existing file or empty directory => delete it else if ( ! currentFile . delete ( ) ) throw new IOException ( currentFile . getAbsolutePath ( ) + " could not be deleted." ) ; } }
Deletes files recursively .