Employee Data Retention and Purge Automation Script
Generate a robust database cleanup and archival script to enforce legal employee data retention periods.
Use this template when automating GDPR or HR compliance data purge routines across relational databases. It produces a production-ready migration script with safety guardrails and transaction management.
Role: Lead Database Reliability Engineer specializing in enterprise compliance and data lifecycle automation.
Context
- Target database engine: {{target_database_engine}}
- Retention window limit: {{retention_period_days}} days
- Schema under management: {{target_schema_name}}
- Affected PII entity tables: {{pii_table_list}}
- Cold archive destination: {{archive_storage_path}}
- Safety execution mode: {{dry_run_flag}}
Task
Draft a fully automated, idempotent data archival and purge script that safely exports expired records to cold storage and deletes them from live operational tables in strict adherence to data governance policies.
Method
- Initialize script parameters, setting execution safeguards based on {{dry_run_flag}}.
- Open an explicit transaction with strict isolation settings tailored to {{target_database_engine}}.
- Validate the availability and schema match of {{target_schema_name}} before modification.
- Calculate the deterministic cutoff timestamp dynamically using {{retention_period_days}}.
- Implement an export step piping eligible records from {{pii_table_list}} into {{archive_storage_path}}.
- Structure batched DELETE statements using chunking to prevent table locking and transaction log exhaustion.
- Insert audit trail records capturing timestamps, row counts deleted, and operator identity.
- Enforce rollbacks on any unhandled error and emit clear operational stdout logs.
Constraints
- The script MUST execute all write operations within chunked transactions to avoid lock escalation.
- Destructive operations MUST NOT run when {{dry_run_flag}} is set to true.
- All database commands must be native and fully compatible with {{target_database_engine}}.
- Sensitive connection credentials must never be hardcoded into the output.
Output format
Provide the response strictly in three parts:
- Execution Summary: Brief 2-3 sentence overview of the script's behavior.
- Complete Executable Script: Single code block containing the end-to-end runnable code with inline comments.
- Rollback & Verification Commands: 3-5 SQL verification queries to check counts post-execution.
Self-review
- Verify that batched deletion loops include pause intervals or commit boundaries.
- Confirm every table in {{pii_table_list}} is addressed with both archival and deletion steps.
- Ensure zero syntax dialect discrepancies against {{target_database_engine}}.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.