How to Run unused dbt models detection from Warehouse Query History
Join your dbt manifest to warehouse read logs and retire the models nobody queries
The reliable way to do unused dbt models detection is to join the model list in dbt's manifest.json to your warehouse's read history, such as Snowflake's ACCESS_HISTORY view, and flag every model that has no downstream dbt dependents and no reads in a fixed window (we use 90 days). dbt's own lineage graph cannot tell you this, because it records what depends on a model, not what anyone actually queries.
Key Takeaways
- •A dbt model is unused only when two things are true: no other model, exposure or test depends on it, and nothing outside dbt has read it within your window.
- •The dbt manifest answers the first question. Warehouse query history answers the second. You need both.
- •Check direct objects accessed, not only base objects, or you will miss reads of views and incremental models exposed through views.
- •Do not drop on the first pass. Rename or disable the model, wait one full business cycle, then delete.
- •Query history is retained for a limited time (Snowflake's account usage views keep 365 days), so a 90-day window is the practical default.
Why unused dbt models detection starts with query history
Most teams try to find dead models by reading the DAG and looking for leaves. That finds models with no children, but a leaf is often exactly what a dashboard, a notebook or a reverse-ETL job reads. A leaf with ten daily readers is a mart. A leaf with zero readers in 90 days is a cost line: you pay to build it on every run and to store it, and your engineers pay in review time every time it breaks.
The opposite mistake is also common. A model with children can still be dead if every child is itself unused. That is why the method below works in two passes: find leaves with no reads, then repeat after removing them, because new leaves appear as you prune.
Step 1: Export the model list and dependency graph from dbt
Run dbt compile or dbt docs generate so that target/manifest.json is current. The manifest lists every node under nodes, with resource_type, database, schema, alias and config.materialized. It also contains a child_map that maps each node to its dependents, and an exposures section for dashboards and applications you have declared.
Extract each model's fully qualified relation name (database.schema.alias, upper-cased for Snowflake) and its direct children. Keep only resource_type = model. Ephemeral models never create a warehouse object, so skip them, and treat their parents as if they feed the ephemeral model's children.
Step 2: Pull last-read timestamps from warehouse query history
On Snowflake, SNOWFLAKE.ACCOUNT_USAGE.ACCESS_HISTORY records which objects each query touched. Per the Snowflake documentation it requires Enterprise Edition or higher and has up to a few hours of latency. Flatten DIRECT_OBJECTS_ACCESSED and take the maximum query start time per object: SELECT obj.value:objectName::string AS relation, MAX(query_start_time) AS last_read FROM snowflake.account_usage.access_history, LATERAL FLATTEN(direct_objects_accessed) obj GROUP BY 1.
Use DIRECT_OBJECTS_ACCESSED rather than BASE_OBJECTS_ACCESSED. The base column resolves views down to the underlying tables, so a view that analysts query every day can look unread if you only inspect base objects. The direct column records the object named in the query. Other warehouses expose equivalents through their job or query logs; the logic is the same, you only change the view you read.
Filter out your own noise before you trust the number. Reads by the dbt service role (tests, snapshots, downstream model builds) are not consumption. Exclude the role or user that runs dbt, your monitoring jobs and any catalog crawler that scans every table nightly. If you skip this step, every model looks used, because your tooling reads all of them.
Step 3: Join the manifest to the read history
Load the manifest extract into a scratch table and left join it to the last-read results on the relation name. Three columns matter in the result: last_read, child_count (from child_map) and exposure_count. Models with a null last_read have not been read since query history began, which is a stronger signal than an old timestamp.
Name-matching breaks in predictable places. Models with custom generate_schema_name macros land in schemas you did not expect, and models with alias overrides differ from the file name. Always join on the compiled relation from the manifest, never on the model's file name.
Step 4: Classify candidates by risk
Sort every model into one of four buckets using the three columns above.
| Children in dbt | Exposures | Reads in window | Action |
|---|---|---|---|
| None | None | None | Retire candidate: disable, then delete |
| None | None | Some | Keep: external consumers exist. Consider declaring an exposure |
| Some | None | None (all children also unread) | Retire the chain from the leaves up, one pass at a time |
| Any | Yes | Any | Keep: someone declared a dependency |
Add two manual guards. First, anything tagged as a seasonal or regulatory model (year-end close, audit extracts) needs a full year of history, not 90 days. Second, check grants: a table granted to an external BI role or a share may be read through a path your query history does not cover, such as a data share consumed in another account.
Step 5: Retire models in two stages
Do not delete on the first pass. In the first PR, set enabled: false in the model's config or move it to a deprecated/ folder with a +materialized: ephemeral override, and leave the warehouse object in place. In the second PR, one full business cycle later (a month-end close is a good test), delete the file and drop the relation. If someone complains in between, re-enabling is a one-line revert.
Estimate the savings before you open the PR. The model's runtime from run_results.json multiplied by run frequency gives build cost, and the table's bytes from the information schema gives storage. We treat any estimate as directional, since shared warehouse compute rarely attributes cleanly to one model.
Step 6: Make the check recurring
A one-time cleanup decays. New models ship every week, and old consumers leave quietly. Schedule the join as a weekly job that writes candidates to a table, and post the list to the team that owns each model. Ownership comes from meta.owner in the model's YAML, so set it on every model if you have not.
This is the part we automate. Our Usage Intelligence Agent (see the agent overview on dataworkers.io) tracks who queried what across the warehouse, scores tables hot or cold, and produces deprecation candidates. Because every Data Workers agent is an MCP server, you can ask it from Claude Code or Cursor in the terminal: "What tables haven't been queried in 90 days?" The Model Context Protocol is what lets the same question run from whichever coding agent your team already uses.
The agent chains with the rest of the platform. Our Catalog Agent) keeps ownership and descriptions current, so each candidate arrives with an owner attached. Our Pipeline Agent can then open the dbt change that disables the model, and the Pro edition routes that write through an approval workflow with an audit trail. The open-source edition is read-only by default, so it will surface candidates but never change your project.
Common mistakes in unused dbt models detection
- •Trusting the DAG alone. Leaves with readers look identical to leaves without them.
- •Counting dbt's own reads. Tests and downstream builds make everything look used.
- •Using too short a window. Quarterly and annual reports vanish from a 30-day view.
- •Ignoring views. Read the direct objects column, or views and their sources look unread.
- •Deleting immediately. Disable first, delete after a full cycle.
Frequently Asked Questions
How do I find unused dbt models without warehouse access to query history? You cannot do it reliably. The dbt manifest shows dependencies only, so you would have to fall back on leaf models with no children, which includes every dashboard-facing mart. If you cannot read query history, ask your platform team for a view over the warehouse's access logs scoped to your schemas.
How long should the window be for unused dbt models detection? We recommend 90 days as the default and 365 days for anything tagged finance, audit or regulatory. This is our opinion rather than a standard. Choose the window from your longest real consumption cycle, and cap it at your warehouse's query history retention.
Will dropping unused dbt models actually reduce cost? It reduces build compute and storage for those models, but the saving depends on how heavy they are. Check run_results.json timings first. Pruning ten sub-second models saves very little, while pruning one large incremental model can matter.
Can an AI coding agent do this for me? Yes, with the right data access. An agent in Claude Code or Cursor can read the manifest and run the join, and a purpose-built agent that already holds the usage history can answer the question directly. Either way, keep a human approval on the change that disables or drops a model.