'How to remove files inside the hadoop directory if there exists any?

I am using below command to do that. Please note this is working fine if there are any contents in that folder

hdfs dfs -rm -r /home/user/folder/*

But am getting an error when the folder is EMPTY ": No such file or directory".

My requirement is it should delete any contents if there exists. How can I achieve that ?



Solution 1:[1]

First run command with -test -d then && to short-circuit the rm command if it doesn't exist.

hadoop fs -test -d /home/user/folder && hadoop fs -rm -r /home/user/folder/*

Alternatively, ignore the error because the path is already deleted.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 OneCricketeer