# BUG: CLI flags do not take precedence over env var DBT\_DEFER\_STATE

**URL:** <https://discourse.getdbt.com/t/bug-cli-flags-do-not-take-precedence-over-env-var-dbt-defer-state/17788>\
**Category:** Help\
**Created:** [January 20, 2025, 4:23pm UTC](https://discourse.getdbt.com/t/bug-cli-flags-do-not-take-precedence-over-env-var-dbt-defer-state/17788 "2025-01-20T16:23:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![scfrouws](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/scfrouws/32/4860_2.png) [@scfrouws](https://discourse.getdbt.com/u/scfrouws)\
**Post date:** [January 20, 2025, 4:23pm UTC](https://discourse.getdbt.com/t/bug-cli-flags-do-not-take-precedence-over-env-var-dbt-defer-state/17788/1 "2025-01-20T16:23:51Z")

</div>

When specifying the env var dbt\_DEFER\_STATE in your terminal, it appears to be considered even when you try to overrule the state manually using `dbt test --defer --state mystate`

The following example results in `stateprod` being used as state.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/4/4619a9d926c091d4c2dac4fb4859b0549d2b7e2a.png)

When doing the same with dbt\_STATE (and unsetting dbt\_DEFER\_STATE), it works as expected, and the flag gets precedence.  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/2X/3/3313d464d922655dc4057ebaf1535ff8fa69ad52.png)

I actually figured I should probably place a bug report. So did that here:

> <https://github.com/dbt-labs/dbt-core/issues/11216>
>
> \### Is this a new bug in dbt-core?
> 
> \- \[x\] I believe this is a new bug in dbt-cor…e
> \- \[x\] I have searched the existing issues, and I could not find an existing issue for this bug
> 
> \### Current Behavior
> 
> When specifying the env var DBT\_DEFER\_STATE=stateprod, it appears to be considered even when you try to overrule the state manually using \`dbt test --defer --state state\` (non-prod)
> 
> Do I miss something regarding 
> 
> \### Expected Behavior
> 
> Expected to be able to overwrite the state using the flags, as stated in the docs: https://docs.getdbt.com/reference/node-selection/syntax#establishing-state
> 
> \### Steps To Reproduce
> 
> The following example results in \`stateprod\` being used as state.
> 
> \<img width="374" alt="Image" src="https://github.com/user-attachments/assets/d028f307-2132-424a-acaf-28f09dc071f6" /\>
> 
> 
> When doing the same with DBT\_STATE (and unsetting DBT\_DEFER\_STATE), it works as expected, and the flag gets precedence. i.e. the used state is 'state'
> 
> \<img width="366" alt="Image" src="https://github.com/user-attachments/assets/6cda0d88-607a-447e-a965-1f22d48b6965" /\>
> 
> 
> \### Relevant log output
> 
> \`\`\`shell
> NA
> \`\`\`
> 
> \### Environment
> 
> \`\`\`markdown
> \- OS: Windows 11
> \- Python: 3.10.5
> \- dbt: 1.8.7
> \`\`\`
> 
> \### Which database adapter are you using with dbt?
> 
> spark
> 
> \### Additional Context
> 
> I noticed that the on-run-end DOES use the expected state (provided via the cli flags) when having DBT\_DEFER\_STATE set! Causing inconsistent catalogs being referred in my case.
