Sub topic 12
Deployment state and cleaning up with destroy
This chapter assumes you can already run databricks bundle validate, deploy, and run against a target, and that you have read the earlier chapters on databricks.yml, resources, and targets. Here you will learn how a bundle remembers what it deployed, how that memory shapes later deployments, and how bundle destroy removes everything safely.
Why a bundle needs state
A bundle describes the resources you want. The workspace holds the resources that exist. To get from one to the other, the CLI must answer three questions on every deploy:
- Which resources did I create last time?
- Which of them changed in the YAML?
- Which of them are no longer in the YAML and should be removed?
The CLI cannot answer these by searching the workspace for a job with a matching name, because names are not unique. It keeps a record called the deployment state. The state maps each resource key in your bundle, such as daily_etl, to the ID of the real object in the workspace, such as a job ID.
Where state lives
Each target gets its own deployment folder in the workspace, called the root path. If you do not set workspace.root_path, the default is a per-user folder:
/Workspace/Users/<your-user>/.bundle/<bundle-name>/<target-name>Under the root path the CLI creates three subfolders:
| Folder | Contents |
|---|---|
| `files` | Your synchronized source files, such as notebooks and Python modules |
| `artifacts` | Built outputs, such as Python wheels |
| `state` | The deployment state and the deployment lock |
The CLI also keeps a local cache in a .databricks folder in your project directory. That cache is a working copy and is rebuilt when needed. Add .databricks/ to .gitignore and never commit it.
Because the authoritative state sits in the workspace, not on your laptop, any machine with access to the root path can deploy the same bundle. A teammate or a CI runner can continue where you left off. Two consequences follow:
- Deleting your local clone does not lose track of deployed resources.
- Deleting or editing the
statefolder in the workspace does make the bundle lose track of them.
The dev target in the earlier example uses the default root path. The prod target sets a shared path so that no single person's home folder owns the deployment:
targets:
prod:
mode: production
workspace:
host: https://<your-prod-workspace-url>
root_path: /Workspace/Shared/.bundle/${bundle.name}/${bundle.target}In production mode the CLI checks that the root path is not tied to one user's folder unless you also set permissions explicitly. This protects you from a deployment that disappears when someone leaves.
How a deploy uses state
During bundle deploy, the CLI compares three things: your configuration, the recorded state, and the current condition of the workspace objects. It then applies only the differences.
Resource keys are identity
The key you give a resource is its identity in the state. In this file the key is daily_etl:
resources:
jobs:
daily_etl:
name: daily_etlThe job's name is a display property. The key is what the state tracks. This has practical effects:
- Changing
nameupdates the existing job in place. - Renaming the key from
daily_etltonightly_etllooks like one resource removed and one added. The next deploy deletes the old job and creates a new one. The new job gets a new ID, and its run history, links, and any external references to the old ID are lost. - Removing a resource from the YAML deletes it from the workspace on the next deploy. You do not need to delete it by hand.
Pick stable keys early and treat renames as a deliberate migration.
Drift
Drift means someone changed a deployed resource outside the bundle, for example by editing a job in the UI. The next deploy overwrites those edits with what the YAML says. This is intended: the bundle is the source of truth. If a change in the UI is worth keeping, copy it into the YAML before you deploy again.
If someone deletes a bundle-managed job in the UI, the next deploy normally recreates it, because the YAML still declares it.
Inspecting what is deployed
Before you change or destroy anything, check what the bundle thinks it owns. bundle summary lists the deployed resources with their IDs and workspace URLs:
databricks bundle summary -t devUse it to confirm that you are looking at the right target and that the resource IDs match what you see in the workspace.
Separate targets, separate deployments
Each combination of bundle name, target name, and root path forms its own deployment with its own state. Two situations commonly surprise people:
- Changing the bundle name or target name points the CLI at a new, empty root path. The next deploy creates a second copy of every resource. The old copies remain, and nothing tracks them as part of the new deployment.
- Changing
root_pathhas the same effect.
Run bundle destroy on the old deployment before you rename, or accept that you will clean up the orphans manually.
In mode: development, resource names are prefixed with [dev <your-username>], and schedules are paused. This keeps your dev copy from colliding with a teammate's copy of the same bundle. Because each user has a separate default root path, each also has a separate state.
Concurrent deploys and the lock
Two deploys writing the same state at the same time could corrupt it. To prevent that, the CLI writes a lock file into the state folder at the start of a deploy or destroy and removes it at the end. A second operation against the same root path fails with a message that the deployment is locked.
A lock can be left behind if a process is killed midway, for example a CI runner that is cancelled. Once you are sure no other operation is running, override the lock:
databricks bundle deploy -t dev --force-lockbundle destroy accepts the same flag. Do not use --force-lock as a habit. It removes the protection that prevents concurrent writes.
Removing a deployment with destroy
databricks bundle destroy deletes what the bundle deployed to one target: the resources recorded in state, plus the deployed files, artifacts, and state under the root path.
# Show what would be removed, then ask for confirmation
databricks bundle destroy -t dev
# Skip the confirmation prompt, for scripts and CI
databricks bundle destroy -t dev --auto-approveWithout --auto-approve, the CLI lists the resources and the folder it will delete, and waits for you to type y. Read that list. It reflects the state, so it is also a quick check that you picked the right target.
Things to know about destroy:
- It acts on one target. Without
-t, it uses the target markeddefault: true, if there is one. Always pass-texplicitly. - It deletes what is in the state, not everything with a similar name.
- It does not delete your local files or your Git repository.
- A user needs permission to delete the resources and the root folder. If the deployment was made under a service principal, you may need to run the destroy as that principal, or hold CAN_MANAGE on the objects.
Protecting production
For production targets, take these precautions:
- Do not put
--auto-approvein a command that a developer can run casually againstprod. - Limit who can run as the production identity and who has CAN_MANAGE on the root path.
- Keep catalogs and storage that hold long-lived data outside the bundle, or manage them in a separate, more restricted bundle.
Bringing existing resources under a bundle
Sometimes a job already exists in the workspace and you want a bundle to manage it. If you declare it in YAML and deploy, the CLI creates a second job, because its state has no record of the first. To link them, use bundle deployment bind:
# Link the bundle resource key "daily_etl" to an existing job ID
databricks bundle deployment bind daily_etl <existing-job-id> -t dev
# Stop managing it without deleting it
databricks bundle deployment unbind daily_etl -t devAfter bind, the next deploy updates that existing job instead of creating a new one. After unbind, the job stays in the workspace but is no longer in the state, so deploy and destroy ignore it. Unbinding is the safe way to hand a resource back to manual management before you destroy a bundle.
A safe cleanup routine
For a dev target that you want to reset, this sequence works well:
databricks bundle summary -t dev
databricks bundle destroy -t dev
databricks bundle deploy -t devThe first command shows what exists. The second removes it and its state. The third creates everything again with fresh IDs.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Deploy reports that the deployment is locked | A previous run was interrupted, or another run is active | Wait for the other run. If none is active, rerun with `--force-lock` |
| Every deploy creates duplicate jobs | The bundle name, target name, or `root_path` changed | Destroy the old deployment, or bind resources to the new one |
| A job's run history vanished | The resource key was renamed | Keep keys stable. Use `bind` to reattach an old job if it still exists |
| Destroy fails with a permissions error | You are not the owner and lack CAN_MANAGE | Run as the deploying identity or get the permission |
| UI edits disappear after a deploy | Drift was overwritten by the YAML | Move the change into the YAML |
Exercise
A teammate renames the resource key daily_etl to nightly_etl in resources/daily_etl.yml and leaves the job's name field unchanged. They run databricks bundle deploy -t prod. What happens to the existing job, and what would you have them do to keep the job's run history?
Show answer
The state has an entry for daily_etl but none for nightly_etl. The CLI sees one resource removed and one added, so it deletes the old job and creates a new one with a new ID, and the old run history is lost, even though the display name is the same. To avoid this, keep the original key daily_etl. If the key must change, first run databricks bundle deployment unbind daily_etl -t prod, rename the key, then run databricks bundle deployment bind nightly_etl <existing-job-id> -t prod and deploy, so the existing job is reused.
Exercise
Where is the authoritative deployment state stored, and why does that let a CI runner deploy a bundle that you first deployed from your laptop?
Show answer
It is stored in the state folder under the target's root path in the workspace, for example <root_path>/state. The local .databricks folder is only a cache. Because the state is in the workspace, any machine with access to the root path reads the same record of resource IDs and can update or delete the same resources.