Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 919
volcano Avatar asked Oct 28 '25 14:10

volcano


1 Answers

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);
}
like image 140
Quetzalcoatl Avatar answered Oct 30 '25 13:10

Quetzalcoatl