Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all files from folder with Databricks dbutils

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

like image 288
Carltonp Avatar asked Jan 07 '19 20:01

Carltonp


People also ask

How do I delete a folder using Dbutils?

rm command (dbutils. Removes a file or directory. To display help for this command, run dbutils. fs. help("rm") .

How do I delete a folder in Databricks?

To delete created folder in notebooks: %fs rm -r foobar.

Which of the following command is used for removing the files in DBFS?

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!"


2 Answers

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)
like image 130
jegordon Avatar answered Sep 19 '22 08:09

jegordon


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))
like image 26
celezar Avatar answered Sep 21 '22 08:09

celezar