Purging Aggregates

All three storage adapters implement the same API:

interface EventStore {
    purgeAggregate(id: string): Promise<void>;
}
ts

The method accepts only the aggregate ID. It does not accept an aggregate class or aggregate name, and event deletion matches the ID across the adapter's event storage.

import { EVENT_STORE, type EventStore } from "@event-nest/core";
import { Inject, Injectable } from "@nestjs/common";

@Injectable()
export class PrivacyService {
    constructor(@Inject(EVENT_STORE) private readonly eventStore: EventStore) {}

    async eraseAccount(accountId: string): Promise<void> {
        await this.eventStore.purgeAggregate(accountId);
    }
}
ts

Deleted State

Each adapter deletes, in order, the aggregate's:

  1. Snapshots, when snapshot storage is enabled.
  2. Events.
  3. Aggregate version record.

Those deletions run in one adapter transaction. If a deletion fails, the transaction rejects instead of intentionally leaving a partially purged stream.

AdapterTransaction usedDeleted records
MongoDBMongoDB client session with withTransactionSnapshot documents, event documents, aggregate document
PostgreSQLKnex database transactionSnapshot rows, event rows, aggregate row
Microsoft SQL ServerKnex database transactionSnapshot rows, event rows, aggregate row

MongoDB deployments must support transactions for this operation, as required by MongoDB client sessions. Snapshot-disabled configurations are supported: the no-op snapshot store participates without requiring snapshot storage.

Idempotent Absence

Purging an unknown, adapter-valid ID succeeds and resolves to undefined. It does not affect unrelated aggregates. This makes repeated purge requests safe with respect to already-absent event-store data.

An invalid ID representation can still fail adapter validation. For example, the MongoDB adapter converts the supplied value to an ObjectId; it requires a 24-character hexadecimal ObjectId string and does not accept UUIDs.

Last update at: 2026/09/02 00:14