# Can't delete with pre\_hook

**URL:** <https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160>\
**Category:** Help\
**Tags:** dbt-core\
**Created:** [September 29, 2023, 3:01pm UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160 "2023-09-29T15:01:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gyom](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@Gyom](https://discourse.getdbt.com/u/Gyom)\
**Post date:** [September 29, 2023, 3:01pm UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160/1 "2023-09-29T15:01:36Z")

</div>

Hello,

In an incremental model, I want to run a delete statement if is\_incremental().  
But the delete prehook is completely ignored, I don’t understand why.

The code of my model (called a\_test.sql):  
{% if is\_incremental() %}  
{% set pre\_hook = “delete from {{ this }}” %}  
{% else %}  
{# At the first run, nothing to do #}  
{% set pre\_hook = “” %}  
{% endif %}

{{ log (“prehook=” ~pre\_hook, info=true)}}

{{  
config(  
materialized=“incremental”,  
pre\_hook=pre\_hook  
)  
}}

{{ log (“prehook2=” ~pre\_hook, info=true)}}

select 1 as my\_col

The log that is generated:  
$ dbt run --model a\_test  
?[0m14:40:54 Running with dbt=1.4.6  
?[0m14:40:56 Found 823 models, 69 tests, 0 snapshots, 0 analyses, 560 macros, 1 operation, 0 seed files, 129 sources, 0 exposures, 0 metrics  
?[0m14:40:56  
?[0m14:41:12 Concurrency: 1 threads (target=‘dev’)  
?[0m14:41:12  
?[0m14:41:12 1 of 1 START sql incremental model GCO.a\_test … [RUN]  
?[0m14:41:12 prehook=delete from {{ this }}  
?[0m14:41:12 prehook2=delete from {{ this }}  
?[0m14:41:14 1 of 1 OK created sql incremental model GCO.a\_test … [?[32mSUCCESS 1?[0m in 1.94s]  
?[0m14:41:14  
?[0m14:41:14 Running 1 on-run-end hook  
?[0m14:41:14 1 of 1 START hook: events\_to\_datavault.on-run-end.0 … [RUN]  
?[0m14:41:14 1 of 1 OK hook: events\_to\_datavault.on-run-end.0 … [?[32mOK?[0m in 0.00s]  
?[0m14:41:14  
?[0m14:41:14  
?[0m14:41:14 Finished running 1 incremental model, 1 hook in 0 hours 0 minutes and 17.98 seconds (17.98s).  
?[0m14:41:14  
?[0m14:41:14 ?[32mCompleted successfully?[0m  
?[0m14:41:14  
?[0m14:41:14 Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1

=\> I can see that the prehook is correctly populated, but when I look in the Snowflake console, I can’t find the delete statement. And no rows are never deleted…  
I simplified the code as much as possible just to have something short and easy to understand.

Anyone to help please?  
Thanks

---

<div class="post-metadata">

**Author:** ![Gyom](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@Gyom](https://discourse.getdbt.com/u/Gyom)\
**Post date:** [October 5, 2023, 10:15am UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160/2 "2023-10-05T10:15:08Z")

</div>

Anyone could help me on this topic please?

---

<div class="post-metadata">

**Author:** ![aschlaegel](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@aschlaegel](https://discourse.getdbt.com/u/aschlaegel)\
**Post date:** [October 18, 2023, 2:23am UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160/3 "2023-10-18T02:23:31Z")

</div>

I believe config happens before anything else in the model - your prehook cannot be set in jinja outside of the config. Rework your prehook into a sql that can discern your criteria for the delete or call a sproc that accomplishes the same thing from the config and remove that from the jinja.

Hope that helps some…

---

<div class="post-metadata">

**Author:** ![Gyom](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@Gyom](https://discourse.getdbt.com/u/Gyom)\
**Post date:** [October 18, 2023, 8:43am UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160/4 "2023-10-18T08:43:20Z")

</div>

OK, thanks. I want to ensure the table exists before trying to delete from it so I’ll have to find a workaround…

---

<div class="post-metadata">

**Author:** ![frank.tubbing](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@frank.tubbing](https://discourse.getdbt.com/u/frank.tubbing)\
**Post date:** [October 28, 2024, 8:06am UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160/5 "2024-10-28T08:06:56Z")

</div>

For anyone stumbling across this topic, I just figured out that if you simply use `dbt build` this problem is resolved.

---

<div class="post-metadata">

**Author:** ![Surya](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/surya/32/3763_2.png) [@Surya](https://discourse.getdbt.com/u/Surya)\
**Post date:** [November 1, 2024, 7:49am UTC](https://discourse.getdbt.com/t/cant-delete-with-pre-hook/10160/6 "2024-11-01T07:49:41Z")

</div>

> [@Gyom](#):
>
> delete from {{ this }}

```auto
{{
config(
materialized=“incremental”,
pre_hook= '{{ "delete from "~this if is_incremental() else "" }}'
)
}}

```

Try this approach
