Sub topic 12
Deployment State and Cleaning Up with destroy
This chapter assumes you can write a databricks.yml, define a job as a resource, and run databricks bundle deploy against a target. You will learn how the CLI remembers what it deployed, how that memory drives updates and deletions, and how to remove a deployment safely.
Why a bundle needs state
When you deploy, the CLI must decide whether each resource in your YAML is new, changed, or gone. It can't answer that by looking at your workspace alone. Someone else may have created a job with the same name, and renaming a job in YAML would otherwise look like a brand-new job.
The CLI solves this by keeping a deployment state for each target. The state records which workspace resources the bundle created and the IDs they received. On the next deploy, the CLI compares your configuration to that record and applies the difference.
Where state lives
State is stored in the workspace, under the target's root_path. By default that is /Workspace/Users/<you>/.bundle/<bundle name>/<target>. Deployed files go in a files folder beside it, and state goes in a state folder.
Because each target has its own root_path, dev and prod keep separate state. Deploying to one never changes the other.
targets:
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}Setting root_path explicitly, as in the production target above, keeps state in a shared location that does not depend on which user runs the deploy.
What state changes in practice
State explains three behaviors that surprise people.
- Renaming a resource key. If you change the key
daily_etltonightly_etlunderresources.jobs, the CLI sees the old key as removed and the new key as added. It deletes the old job and creates a new one. The new job gets a new ID, so run history and any external links to the old job are lost. - Removing a resource from YAML. Delete the job's block and deploy, and the CLI deletes the job from the workspace.
- Editing in the UI. Changes made in the workspace UI are not tracked in state. The next deploy overwrites them with your YAML.
Treat the YAML as the single source of truth, and make changes there.
Destroying a bundle
databricks bundle destroy deletes everything the bundle deployed to one target. That includes the resources listed in state, the deployed files, and the state itself.
# See what would be removed, then confirm interactively
databricks bundle destroy -t dev
# Skip the confirmation prompt, for scripts
databricks bundle destroy -t dev --auto-approveWithout --auto-approve, the CLI lists the resources and files it will delete and waits for your confirmation. Read that list before you accept it.
Destroy removes only what the bundle created. It does not touch tables your job wrote to, or other jobs in the workspace. Data your pipelines produced stays where it is, so clean it up separately if you need to.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A job was recreated with a new ID after deploy | The resource key was renamed in YAML | Keep keys stable. Rename the `name` field instead. |
| Deploy fails because a file or lock is held | Another deploy is running, or a previous one was interrupted | Wait for the other run, or retry. Use `--force-lock` only when you are sure nothing else is deploying. |
| Destroy reports nothing to delete | Wrong target, or the target's `root_path` changed | Check `-t` and the `root_path`, since state is found through it. |
| Changes made in the UI disappeared | The next deploy overwrote them | Make the change in YAML and redeploy. |
Changing root_path after a deploy is a trap. The CLI looks for state at the new path, finds none, and treats everything as new. The old deployment stays in the workspace, and you now have duplicate jobs. If you must move, run destroy with the old path first, then deploy with the new one.
Exercise
You rename the job key daily_etl to nightly_etl in resources/daily_etl.yml, leave the job's name field unchanged, and run databricks bundle deploy -t prod. What happens to the existing job, and how would you avoid it?
Show answer
The CLI tracks resources by key. It sees daily_etl as removed and nightly_etl as new, so it deletes the old job and creates a new one with a new ID, losing the old run history. To avoid it, keep the key daily_etl and change only the name field if you want a different display name.
Exercise
Why does databricks bundle destroy -t dev leave a Delta table that the job wrote untouched?
Show answer
Destroy deletes only the resources and files recorded in the bundle's deployment state. A table created by running the job's code is not a bundle resource, so it is not in state and must be dropped separately.