dbt Cheat Sheet
dbt (data build tool) is extensively utilized for data transformation in the analytics engineering field, operating on top of data in warehouse technologies. dbt enables data analysts and engineers to transform, test, and document data in the environment of the warehouse itself. This dbt Cheat Sheet offers a comprehensive reference to the most commonly used dbt commands, including their arguments, and graph and set operators.
Anatomy of a dbt command
A dbt command typically includes a primary action (such as run, test, or compile), which can be accompanied by a series of arguments and operators that help to selectively execute and manage your data transformations:
- Command: The primary action dbt is to perform, like
run
ortest
. - Arguments: Options such as
--select
or--exclude
that specify which models or tests to include or omit. - Graph Operators: Symbols like
+
or@
that define relationships and dependencies between models, allowing you to include ancestors (upstream models) or descendants (downstream models). - Set Operators: Used within arguments like
--select
to combine or intersect sets of models.
Example command:
_10# Run all models in models/staging, while excluding any models tagged as 'deprecated'_10dbt run --select models/staging --exclude tag:deprecated
dbt Arguments
dbt supports a range of command-line arguments that enable selective and efficient execution of data transformation tasks.
These arguments, such as --select
, --exclude
, --state
, and --defer
help manage and optimize dbt runs by targeting specific nodes or leveraging past run artifacts.
--select
models
, tests
, seeds
) to run. Use node names, tags, or paths.
_10dbt run --select sales_model
--exclude
_10dbt run --exclude long_running_model
--resource-type
model
, seed
, test
, snapshot
).
_10dbt ls --resource-type model
--selector
_10dbt run --selector weekly_jobs
--state
_10dbt run --select state:modified+ --state ./path/to/artifacts
--defer
--state
flag to locate previous artifacts.
_10dbt run --select revenue_forecast+ --defer --state ./path/to/prod/artifacts
--empty
_10dbt run --empty
-–full-refresh
_10dbt run --full-refresh
dbt Graph and Set Operators
The Graph and Set operators provide powerful methods for selectively running transformations based on complex relationships and criteria within your data models.
Graph Operators
+
at the start to select ancestors, at the end to select descendants, or both to select both.
_10# select `sales_model` and all its descendants_10dbt run --select "sales_model+" _10_10# select `user_engagement` and all its ancestors_10dbt run --select "+user_engagement"_10_10# select `daily_metrics`, its ancestors, and descendants_10dbt run --select "+daily_metrics+"
+
operator to specify exact generations of ancestors or descendants.
_10# select `customer_demographics` and its descendants up to two levels down_10dbt run --select "customer_demographics+2"_10_10# select `revenue_forecast` and its first-degree ancestors _10dbt run --select "1+revenue_forecast"_10_10# select `order_items`, two levels of ancestors, and three levels of descendants_10dbt run --select "2+order_items+3"
_10# select `marketing_spend`, its descendants, and the ancestors of its descendants_10dbt run --select "@marketing_spend"
Set Operators
_10# run `inventory_levels`, all its ancestors, and `customer_retention`, and all its ancestors_10dbt run --select "+inventory_levels +customer_retention"
_10# run common ancestors of both `monthly_sales` and `quarterly_goals`_10dbt run --select "+monthly_sales,+quarterly_goals"_10_10# select common descendants of `product_details` and `category_summaries` _10dbt run --select "product_details+,category_summaries+"_10_10# run models in the `marts/supply_chain` directory tagged `urgent`_10dbt run --select "marts.supply_chain,tag:urgent"
dbt Commands
Essentials
Fundamental commands that are commonly used in daily operations and critical for routine dbt tasks.
accepts: --select
, --exclude
, --selector
, --resource-type
, --defer
, --empty
, --full-refresh
_10# run `sales_model` and all its descendants_10dbt run --select "sales_model+" _10_10# Run all models tagged as 'daily' while excluding a specific model_10dbt run --select tag:daily --exclude "long_running_model"
accepts: --select
, --exclude
, --selector
, --defer
_10# Run all tests in a project_10dbt test_10_10# Run only unit tests_10dbt test --select test_type:unit_10_10# Exclude specific tests from execution_10dbt test --exclude "stale_data_check"
accepts: --select
, --exclude
, --selector
_10# Load all seed files in the project_10dbt seed_10_10# Load specific seed file with detailed output_10dbt seed --select "regions" --show_10_10# Refresh all seed data while providing detailed logging_10dbt seed --full-refresh --show
accepts: --select
, --exclude
_10# Run all snapshot jobs within a project_10dbt snapshot_10_10# Execute a specific snapshot job_10dbt snapshot --select "my_snapshot"_10_10# Exclude a snapshot from execution_10dbt snapshot --exclude "old_snapshot"
accepts: --select
, --exclude
, --selector
, --resource-type
, --defer
, --empty
, --full-refresh
_10# Execute a complete build of all project components_10dbt build_10_10# Execute a full-refresh build, reprocessing all incremental models_10dbt build --full-refresh_10_10# Perform a schema-only dry run to validate model deps without processing data_10dbt build --empty
accepts: --select
, --no-compile
, --empty-catalog
_11# Generate documentation for all project components_11dbt docs generate_11_11# Serve the generated documentation locally on a custom port_11dbt docs serve --port 8081_11_11# Generate documentation without re-compiling models_11dbt docs generate --no-compile_11_11# Generate documentation with limited node inclusion_11dbt docs generate --select "tag:critical"
Advanced
Commands that are used for specific scenarios that may require a more detailed understanding of dbt functionalities or involve complex operations.
zero-copy cloning
where available. Supports use cases such as blue/green deployments
and development schema updates.accepts: --state
, --select
, --full-refresh
, --threads
_10# Clone all models from a specified state to your target schemas_10dbt clone --state path/to/artifacts_10_10# Clone a model from a past state and refresh all relationships in the target env_10dbt clone --select "target_model" --state path/to/artifacts --full-refresh_10_10# Clone models with increased parallel processing using 4 threads_10dbt clone --state path/to/artifacts --threads 4
accepts: --args
_10# Execute a macro to grant select permissions with specified arguments_10dbt run-operation grant_select --args '{role: "analyst"}'_10_10# Run a maintenance macro to clean up old models, specifying days and dry_run status_10dbt run-operation cleanup_old_models --args '{days: 30, dry_run: False}'_10_10# Perform a custom operation to update data partitions_10dbt run-operation update_partitions --args '{partition_date: "2023-01-01"}'
accepts: --select
, --exclude
, --selector
, --output
_10# Check the freshness of all sources in the project_10dbt source freshness_10_10# Validate freshness for a specific source table_10dbt source freshness --select "source:google_ads.campaign"_10_10# Output freshness results to a custom JSON file_10dbt source freshness --output custom_freshness_results.json
Helpers
Utility commands that assist with project setup, troubleshooting, and maintenance, which might not be used daily but are essential for project health and optimization.
accepts: --profile
_10# Initialize a new dbt project with a specific profile_10dbt init my_new_project --profile my_profile_10_10# Create a new dbt project with default settings_10dbt init my_fresh_project
packages.yml
file.accepts: --no-partial-parse
, --upgrade
, --add-package
, --source
_10# Update and reinstall all dependencies_10dbt deps --upgrade_10_10# Add or update a specific package configuration_10dbt deps --add-package dbt-labs/dbt_utils@1.2.0 --source git_10_10# Install a package from a local directory_10dbt deps --add-package /path/to/local/package --source local
accepts: --resource-type
, --select
, --models
(Similar to --select
+ --resource-type=model
), --exclude
, --selector
, --output
, --output-keys
_10# List all models in JSON format_10dbt list --resource-type model --output json_10_10# List all sources with a specific tag in name format_10dbt list --select tag:raw --output name_10_10# Exclude specific models and list the rest_10dbt list --exclude "stg_orders" --output path
clean-targets
section of the dbt_project.yml
. Useful to clean up dbt_packages
and target
directories.accepts: None
_10dbt clean
target/
directory.accepts: --select
, --exclude
, --selector
, --inline
_10# Compile a specific staging model to review its SQL_10dbt compile --select "stg_orders"_10_10# Compile an inline SQL statement referencing a raw customers table_10dbt compile --inline "select * from {{ ref('raw_orders') }}"
accepts: --config-dir
_10# Run a basic debug to test connection and environment setup_10dbt debug_10_10# Show configured location for the profiles.yml file_10dbt debug --config-dir
accepts: --no-partial-parse
_10# Perform a standard parse of the dbt project_10dbt parse_10_10# Force a complete re-parse of the entire project, ignoring any cached results_10dbt parse --no-partial-parse
accepts: None
_12# Retry the last dbt command after a failure_12dbt retry_12_12# Example where the retry command successfully reruns a failed model after the error is fixed_12dbt run --select "faulty_model"_12# Assume "faulty_model" fails due to a syntax error_12dbt retry # After fixing the syntax error_12_12# Example demonstrating idempotent behavior when no changes are made after a failure_12dbt run --select "stable_model"_12# Assume "stable_model" fails and no changes are made to address the failure_12dbt retry # Will likely fail again without changes
accepts: --select
, --inline
, --limit
_10# Preview the first 5 rows from a specific model's output_10dbt show --select "stg_customers"_10_10# Execute and display results of an arbitrary query from a model_10dbt show --inline "select * from {{ ref('stg_products') }}"_10_10# Display a custom number of rows for a model's preview_10dbt show --select "stg_orders" --limit 10