Delete files from workspace databricks with Python

89 Views Asked by At

I have a group of files in the Workspace of DataBricks that I would like to delete in group. I am using this Python script:

from pyspark.sql import *  #not sure if it is necesary to delete the files
dbutils.fs.rm('/Workspace/Users/[email protected]/*.json', True)

The answer I get is False below the Notebook and it doesn't delete any file.

Is there something wrong with my script?

1

There are 1 best solutions below

0
Kashyap On

dbutils.fs is for interacting with dbfs, not workspace.

You can use python file i/o if you want to interact with workspace.

So in your case it would be:

from pathlib import Path

for f in list(Path('/Workspace/Users/[email protected]/').glob('*.json')):
    Path.unlink(f)

# Older python version
import os, glob
for f in glob.glob('/Workspace/Users/[email protected]/*.json'):
    os.remove(f)