volcano
volcano

Reputation: 91

Is it possible to delete multiple files at a time from hdfs using hadoop filesystem API?

hadoop shell has the command hadoop fs -rm /dir/*. But I don't find a similar one from API.

Upvotes: 4

Views: 6660

Answers (1)

Quetzalcoatl
Quetzalcoatl

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

Related Questions