Reputation: 91
hadoop shell has the command hadoop fs -rm /dir/*
. But I don't find a similar one from API.
Upvotes: 4
Views: 6660
Reputation: 3067
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);
}
Upvotes: 3