---
id: WF-PFM-MTD-011
title: Sweep the business event ledger nightly
trigger: Nightly ledger cleanup limits retained facts to the configured window while protecting events with waiting
  outbox work..
product_area: platform
module: metadata
revision: 1
status: draft
design_intent: proposed
implementation_evidence: observed
inference: []
source_sha:
  services-platform: ad3f6a6582843008ed0f5a17b131878d15829af1
implements:
- CAP-PFM-MTD-015
moves:
- STATE-PFM-MTD-008
crosses:
- SVC-PFM-MTD-custom-objects
idempotency: A repeated sweep removes only remaining qualifying facts. The single dispatcher concurrency slot excludes
  simultaneous sweep and dispatch invocations.
concurrency: A repeated sweep removes only remaining qualifying facts. The single dispatcher concurrency slot excludes
  simultaneous sweep and dispatch invocations.
hops:
- n: 1
  actor_or_service: Scheduled rule
  action: The nightly rule invokes the dispatcher’s sweep action at 05:20 UTC.
  anchor: services-platform:modules/custom-objects/aws/logic/business-events.lambda.ts:BUSINESS_EVENTS_SWEEP_CRON
  seam: schedule
  on_failure: Invocation failure delays cleanup until a later successful run.
  left_inconsistent: Old ledger entries can remain past the retention window.
  observer: Scheduled execution logs and configured alarms.
  provenance: observed
  source: services-platform:modules/custom-objects/aws/logic/business-events.lambda.ts:createBusinessEventsFunctions
- n: 2
  actor_or_service: Business events dispatcher (sweep)
  action: The sweep validates whole-day retention and removes old facts lacking outbox references, logging the count.
  anchor: services-platform:modules/custom-objects/services/persistence/business-events.ts:sweepBusinessEvents
  seam: sync
  on_failure: Roll back the sweep transaction on failure.
  left_inconsistent: No partial removal commits in that sweep.
  observer: The initiating person or service and its operation logs.
  provenance: test
  source: services-platform:modules/custom-objects/tests/integration/services/persistence/business-events.integration.spec.js#refuses
    a retention shorter than a day
branches: []
waits: []
evidence:
- 'services-platform at ad3f6a65: modules/custom-objects/aws/lambda/BUSINESS_EVENTS_DISPATCHER.handler.ts; aws/logic/business-events.lambda.ts;
  business-events.settings.ts; services/persistence/business-events.ts; docs/business-events.md §3'
controls: [TC-AVL-004, TC-AVL-005, TC-AZ-001, TC-AZ-002, TC-AZ-003, TC-AZ-004, TC-DAT-001, TC-DAT-002, TC-DAT-003, TC-DAT-004, TC-IAM-001, TC-IAM-002, TC-IAM-004, TC-IAM-005, TC-IAM-006, TC-LOG-004, TC-LOG-005, TC-MON-001, TC-MON-002, TC-PI-001, TC-PI-002, TC-PI-003, TC-PI-004, TC-PI-005, TC-TEN-001, TC-TEN-002, TC-TEN-003, TC-VEN-001, TC-VEN-002, TC-VEN-003]
---

# Sweep the business event ledger nightly

## What It Does

Nightly ledger cleanup limits retained facts to the configured window while protecting events with waiting outbox work.

## The Flow

```mermaid
sequenceDiagram
  participant Rule as Nightly rule (05:20 UTC)
  participant Disp as Dispatcher (sweep action)
  participant Ledger as Ledger and outbox
  Rule->>Disp: invoke with sweep
  Disp->>Ledger: remove entries older than 90 days not named by the outbox
  Ledger-->>Disp: count removed
  Disp->>Disp: log count
```

## When Something Fails

Invocation failure delays cleanup until a later successful run. Old ledger entries can remain past the retention window.

## Open Design Questions

The retention period and replay expectations still lack a product decision.
