public static boolean delete ( final File file ) { boolean delete = false ; if ( file != null && file . exists ( ) ) { // even if is a directory try to delete. maybe is empty or maybe is a *nix symbolic link delete = file . delete ( ) ; if ( ! delete && file . isDirectory ( ) ) { File [ ] childs = file . listFiles ( ) ; if ( childs != null && childs . length > 0 ) { for ( File child : childs ) { delete ( child ) ; } // then try again as by now the directory can be empty delete = file . delete ( ) ; } } } return delete ; }
Deletes the file or recursively deletes a directory depending on the file passed .