Cascading Deletes

Automatically delete related Convex documents in one call using your existing indexes to define parent-child relationships.

Installation

npm install @sholajegede/convex-cascading-deletes

About Cascading Deletes

Drop-in Convex component for cascading deletes across related documents. Declare relationships once using your existing indexes, then call deleteWithCascade() instead of ctx.db.delete(). Dependents are recursively found and deleted in scheduler-batched mutations — atomic per batch. Includes a getSafeDb() helper that patches ctx.db to throw on direct deletes, a reactive deletion log queryable via useQuery, circular relationship protection, and index validation at startup.

Benefits

Use cases

how to delete user and all related data in Convex

The Cascading Deletes component lets you delete a user and cascade to all their posts, comments, and other related records automatically. Configure relationships via your existing indexes, then call deleteWithCascade() instead of manual cleanup across multiple tables.

Convex referential integrity foreign key constraints

While Convex doesn't have built-in foreign key constraints, this component provides referential integrity through cascading deletes. Define parent-child relationships using your existing indexes and ensure dependent records are always cleaned up when parents are deleted.

batch delete related documents Convex scheduler

Large deletion trees are automatically processed in batches via the Convex scheduler, with atomic guarantees within each batch. The component handles the complexity of recursive dependency resolution and maintains deletion logs for audit purposes.

Frequently asked questions

How does the Cascading Deletes component handle circular relationships?

The Cascading Deletes component includes built-in circular relationship protection using visited tracking to prevent infinite loops. This ensures that complex relationship graphs with circular dependencies can be safely deleted without causing stack overflows or endless deletion cycles.

Do I need to modify my existing Convex schema to use cascading deletes?

No schema changes are required. The Cascading Deletes component uses your existing indexes to define relationships between tables. You simply configure which indexes represent parent-child relationships and the component handles the rest automatically.

What happens if a cascading delete operation fails partway through?

Deletions within each scheduler batch are atomic, so partial batches won't leave your database in an inconsistent state. If a batch fails, only that specific batch is rolled back while previous successful batches remain committed. The component logs all operations for audit and debugging purposes.

Can I query which records were deleted in a cascading operation?

Yes, the Cascading Deletes component maintains a reactive deletion log that tracks every cascade operation with per-table counts and timestamps. You can query this log using useQuery to see exactly which records were deleted and when the cascade ran.

Links