---
title: "Automatic delete detection logic"
canonical: "https://docs.vaultspeed.com/space/VPD/3364389942/Automatic%20delete%20detection%20logic"
format: markdown
---
> Macro (toc)

VaultSpeed can automatically generate extra logic to detect deleted records if your load type is any of the ‘Full Load ….. with delete detection’ types.

In this case, VaultSpeed creates extra objects in the ‘STAGING’ layer where we keep track of all previously loaded records and the newly loaded ones.  These extra objects are the ‘STG_PREV’ and ‘STG_CURR’ objects, which will hold, respectively, the previous or current data.  Where the difference between the two will indicate the records that are deleted on the source.  This difference calculation is done in an extra mapping that VaultSpeed will generate: the PREP_DELETE mapping.

> 📝 Note that these STG_CURR and STG_PREV objects will never get dropped/recreated during Delta generations to not lose the history of this data for your next run.  Only ‘alter table’ statements will be generated for these objects.

### Example Flow

Step 0: Starting state.  In the STAGING layer (SCHEMA_STG) you have 3 objects per source object: the STG-object, STG-CURR object, and STG_PREV object.  Let’s assume you have already loaded some data in load 6. 

![image](media://411aad49-5b1b-48f7-a7c5-681df7edbd81)

Step 1: a new load (number 7) is started and delivers data to the EXTRACT layer.

![image](media://b405d18a-bae8-40cf-8a5a-0608cd59f4a5)

Step 2: the STG_PREV object is cleared and populated with the data from the previous load (nr 6)

![image](media://71c0a553-eb7b-4178-9e94-0a1752a898ce)

Step 3: the new data from load nr 7 is loaded to the STG_CURR object.

![image](media://6443180e-212b-4b20-9d4e-eae4b44c4a29)

Step 4: in the PREP_DELETE mapping, the data from the previous and current load is compared. All records that exist in the previous dataset but not in the current dataset are marked for deletion and will provide a logical delete in the data vault for those records.

![image](media://68a0818a-d691-46ef-a9a2-c26a6e1adae6)

### Migration

Customers that used to have loads that use the “full load with delete detection” type will automatically get some migration scripts that create these new ‘STG_CURR’ and ‘STG_PREV’ objects and copies over the current dataset so they can run the new way of delete detection already in the next run.

During your first delta generation, you will get a notification that these extra ‘migration' scripts are generated.  You can find them in a migration folder in the generated zip file.  During the code deployment, these scripts will automatically create the new necessary objects and do the data move.  This means that, after deployment, you can just run your loads as usual, and the delete detection is now fully updated to the new way of working. 

> ⚠️ After you have done this migration, make sure to indicate in VaultSpeed in the DDL/ETL Generation → Migrations screen that you have executed the migration.  If you don’t do this, VaultSpeed will regenerate these migration scripts with every delta generation.

> ℹ️ This is a one-time migration for all objects that use this load type.