Can someone let me know how to use the databricks dbutils to delete all files from a folder. I have tried the following but unfortunately, Databricks doesn't support wildcards.
dbutils.fs.rm('adl://azurelake.azuredatalakestore.net/landing/stageone/*')
Thanks
rm command (dbutils. Removes a file or directory. To display help for this command, run dbutils. fs. help("rm") .
To delete created folder in notebooks: %fs rm -r foobar.
Use %fs magic commands # List the DBFS root %fs ls # Recursively remove the files under foobar %fs rm -r foobar # Overwrite the file "/mnt/my-file" with the string "Hello world!" %fs put -f "/mnt/my-file" "Hello world!"
According to the documentation, the rm function receives 2 parameters :
rm(dir: String, recurse: boolean = false): boolean -> Removes a file or directory
Where the second parameter is a boolean flag to set the recursitivity, so you just need to set it to true:
dbutils.fs.rm('adl://azurelake.azuredatalakestore.net/landing/stageone/',True)
Something like this should work:
val PATH = "adl://azurelake.azuredatalakestore.net/landing/stageone/"
dbutils.fs.ls(PATH)
.map(_.name)
.foreach((file: String) => dbutils.fs.rm(PATH + file, true))
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