What does the dbt clean command do and when should I run it?

dbt clean deletes a project’s generated artifacts to restore the repository to a fresh, reproducible state.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What Is dbt clean?

dbt clean is a command-line utility that removes the target, dbt_packages, and logs directories from a dbt project, returning the workspace to its original, version-controlled state.

Why Does dbt clean Matter?

Cleaning eliminates stale compiled SQL, broken dependencies, and local logs that can mask production issues, ensuring deterministic builds across machines and CI pipelines.

Which Folders Does dbt clean Delete?

The command deletes target/ (compiled models, manifests), dbt_packages/ (packages installed via dbt deps), and logs/ by default, but leaves source-controlled files untouched.

When Should You Run dbt clean?

Run dbt clean before switching branches, after upgrading dbt versions or dependencies, and in CI jobs prior to dbt deps and dbt run to guarantee a pristine environment.

How Do You Execute dbt clean?

From the project root, execute dbt clean. Optionally combine with dependency installation:

dbt clean && dbt deps && dbt run

How Does dbt clean Work Internally?

dbt iterates over a hard-coded list of directories, verifies their presence, and performs recursive deletion using Python’s shutil.rmtree(). No database objects are touched.

Can You Customize dbt clean?

Yes. Add extra paths under the clean-targets key in dbt_project.yml:

clean-targets:
- target
- dbt_packages
- logs
- .pytest_cache

How Does dbt clean Fit into CI/CD?

Insert dbt clean as the first step of every CI job so ephemeral containers start with no leftover artifacts, preventing false-positive test results and cache pollution.

What Are Best Practices for dbt clean?

Commit clean-targets to version control, cache only seeds and snapshots, and treat clean as idempotent—run it anytime without fear of data loss.

What Are Common Pitfalls?

Deleting untracked data files, forgetting to re-install packages, and assuming clean affects the warehouse schema are frequent mistakes; configure clean-targets carefully.

How Does dbt clean Work in Galaxy?

Galaxy’s built-in terminal lets you run dbt clean next to your SQL. The AI copilot detects the command and suggests running dbt deps afterward, streamlining local resets.

Why dbt clean is important

Cleaning ensures that every dbt run starts from the same baseline across developers and pipelines. By deleting generated artifacts, teams avoid "works on my machine" issues, reduce merge conflicts on compiled SQL, and guarantee that dependency changes are fully respected in downstream builds.

dbt clean Example Usage


$ dbt clean && dbt deps && dbt run --select orders

dbt clean Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

Does dbt clean delete my warehouse tables?

No. The command only deletes local folders like target/, dbt_packages/, and logs/. All database objects remain intact.

Can I add custom directories to dbt clean?

Yes. Define extra paths under clean-targets in dbt_project.yml to extend what gets removed.

How often should I run dbt clean?

Run it before every branch switch, after dependency upgrades, and at the start of each CI job for reproducible builds.

How does Galaxy assist with dbt clean?

Galaxy’s SQL editor includes a terminal where you can run dbt clean. The AI copilot recommends follow-up commands like dbt deps, ensuring a smooth workflow.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.