hadoop shell has the command hadoop fs -rm /dir/*. But I don't find a similar one from API.
To delete the files in a directory without deleting the directory itself or deleting files from any sub-directories (i.e. what hadoop fs -rm /dir/* does) you can use:
FileSystem fs = dir.getFileSystem(getConf());
RemoteIterator<LocatedFileStatus> it = fs.listFiles(dir, false);
while (it.hasNext()) {
    fs.delete(it.next().getPath(), false);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With