public static boolean fullyDelete ( File dir ) throws IOException { boolean deleted = true ; File contents [ ] = dir . listFiles ( ) ; if ( contents != null ) { for ( int i = 0 ; i < contents . length ; i ++ ) { if ( contents [ i ] . isFile ( ) ) { if ( ! contents [ i ] . delete ( ) ) { deleted = false ; } } else { //try deleting the directory // this might be a symlink boolean b = false ; b = contents [ i ] . delete ( ) ; if ( b ) { //this was indeed a symlink or an empty directory continue ; } // if not an empty directory or symlink let // fullydelete handle it. if ( ! fullyDelete ( contents [ i ] ) ) { deleted = false ; } } } } return dir . delete ( ) && deleted ; }
Delete a directory and all its contents . If we return false the directory may be partially - deleted .