Anvil.Workers.RetentionSweep (Anvil v0.1.1)
View SourceBackground job worker that enforces data retention policies.
This worker runs periodically (daily at 3 AM) to clean up old data according to retention policies. It handles:
- Deletion of audit logs older than the retention period (default: 7 years)
- Redaction of PII in labels past their retention period (field-level retention)
- Cleanup of expired assignments and temporary data
PII Retention Actions
The worker supports three retention actions for labels with expired PII fields:
:field_redaction- Redact only expired fields, keep unexpired (default):soft_delete- Keep metadata, strip entire payload:hard_delete- Permanent deletion (breaks reproducibility)
Configuration
Queue: :maintenance
Max Attempts: 3
Priority: 2 (medium priority)
Scheduled Execution
Configured in config.exs via Oban.Plugins.Cron:
{"0 3 * * *", Anvil.Workers.RetentionSweep}Job Arguments
retention_days(optional) - Number of days to retain audit logs (default: 2555 = ~7 years)dry_run(optional) - If true, only counts records without deleting (default: false)pii_retention_action(optional) - Retention action for PII fields (:field_redaction,:soft_delete,:hard_delete) (default::field_redaction)process_pii(optional) - If true, process PII retention (default: true)
Telemetry
Emits the following telemetry events:
[:anvil, :workers, :retention_sweep, :started]- Job execution started[:anvil, :workers, :retention_sweep, :completed]- Job completed successfully
Summary
Functions
@spec delete_old_audit_logs(DateTime.t(), boolean()) :: {non_neg_integer(), nil}
Deletes audit logs older than the specified cutoff date.
Parameters
cutoff- DateTime before which logs should be deleteddry_run- If true, only counts without deleting
Returns
A tuple {count, nil} where count is the number of records deleted (or counted in dry run mode).
@spec enqueue(keyword()) :: {:ok, Oban.Job.t()} | {:error, term()}
Enqueues a retention sweep job.
Options
:retention_days- Number of days to retain audit logs (default: 2555):dry_run- If true, only counts records without deleting (default: false):process_pii- If true, process PII retention (default: true):pii_retention_action- Retention action for PII (default: :field_redaction)