Search code examples
databricksdatabricks-cli

Databricks CLI - delete folder if exists


Databricks throws an error when I try to delete a folder that doe not exist:

databricks workspace delete -r /Shared/myfolder

Error message:

Error: b'{"error_code":"RESOURCE_DOES_NOT_EXIST","message":"Path (/Shared/myfolder) doesn\'t exist."}'

So I would probably need to check if the folder exists before deleting it?

Pseudo code example:

if [ -d "/Shared/myfolder" ]; then databricks workspace delete -r /Shared/myfolder ; fi

How can I implement this using Databricks CLI?


Solution

  • There is no separate function in the CLI (and REST API) to check existence of resource. You have two choices:

    1. Just ignore the error - if you don't want to see it in the script, just add > /dev/null at the end of command

    2. Use ls subcommand to check existence of directory, and then delete (but I personally don't see benefit from that). Something like this:

    FOLDER=/Shared/myfolder
    databricks workspace ls $FOLDER > /dev/null
    RES=$?
    if [ $RES -eq 0 ]; then
      databricks workspace delete -r $FOLDER
    fi
    

    I would personally go with first approach