Skip to main content

Clean up branches

As in Git, branches proliferate, and without some housekeeping the lakehouse becomes hard to navigate. Bauplan adds to the count on its own: every job that writes runs in a transactional branch to guarantee atomicity. When a run, import, or table creation fails, Bauplan keeps that branch on purpose, with its partial writes, so you can inspect what went wrong. Nothing removes these branches automatically, nor the ones your own jobs create, until you delete them. It is a good practice to have a scheduled task to prune them.

Delete the branch in the job that created it

First, decide what should happen to the branch when a run fails: delete it, or keep it to inspect the failure.

To delete it, use a finally block, so the branch goes away whether the job succeeds, fails or is cancelled:

from datetime import datetime, timezone

import bauplan

client = bauplan.Client()

stamp = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S")
branch = f"{client.info().user.username}.orders-transformation-prod-{stamp}"

client.create_branch(branch, from_ref="main")
try:
state = client.run(project_dir="pipelines/orders", ref=branch)
if state.job_status != "SUCCESS":
raise RuntimeError(f"run failed: {state.error}")
client.merge_branch(source_ref=branch, into_branch="main")
finally:
# Runs on success, on failure and on cancellation alike
client.delete_branch(branch, if_exists=True)

To keep the branch of a failed run, delete it only on success, and let the scheduled pruning remove it later.

Prune transactional branches on a schedule

caution

Pruning the branches of other users requires the admin role.

Keep a transactional branch only as long as you need it to inspect the partial state of a failed job, as in how to debug a failed pipeline, then delete it. Transactional branches are named after the branch the job ran against, followed by -bpln-tx-<job type>-<timestamp>-<job id>, for example -bpln-tx-run-... for a pipeline run. The find transactional branches script lists them, and the prune transactional branches script deletes them, for instance every night.