Skip to content

Remove read models and children in Cratis Chronicle projections

Use RemovedWith when an event means a read model or child item should no longer appear in a Cratis Chronicle model-bound projection. For a child shared across several parents, RemovedWithJoin removes the matching item from every parent that contains it. Root-level removal through a join is not supported.

In C#, apply RemovedWith to the root read model type and name the event that removes the entire instance:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
[EventType]
public record MbRemovalAccountOpened(string Name, decimal Balance);
[EventType]
public record MbRemovalAccountClosed;
[RemovedWith<MbRemovalAccountClosed>]
public record MbRemovalAccount(
[Key]
Guid Id,
[SetFrom<MbRemovalAccountOpened>(nameof(MbRemovalAccountOpened.Name))]
string Name,
[SetFrom<MbRemovalAccountOpened>(nameof(MbRemovalAccountOpened.Balance))]
decimal Balance);

View C# snippet source on GitHub

When MbRemovalAccountClosed is processed, Chronicle removes the MbRemovalAccount whose key matches the event source ID. This removes the projected read model, not its event history.

If the removal event’s source ID is not the model’s key, set key to the event property that identifies the model:

[EventType]
public record MbRemovalWithKeyAccountOpened(string Name);
[EventType]
public record MbRemovalWithKeyAccountClosed(Guid AccountId);
[RemovedWith<MbRemovalWithKeyAccountClosed>(key: nameof(MbRemovalWithKeyAccountClosed.AccountId))]
public record MbRemovalWithKeyAccount(
[Key]
Guid Id,
[SetFrom<MbRemovalWithKeyAccountOpened>(nameof(MbRemovalWithKeyAccountOpened.Name))]
string Name);

View C# snippet source on GitHub

The custom-key example uses MbRemovalWithKeyAccountClosed.AccountId to select the account, so the event can come from a different source.

Apply more than one RemovedWith attribute when several events can end the model’s lifetime:

[EventType]
public record MbRemovalMultipleAccountOpened(string Name);
[EventType]
public record MbRemovalMultipleAccountClosed;
[EventType]
public record MbRemovalMultipleAccountMerged(Guid SourceAccountId);
[RemovedWith<MbRemovalMultipleAccountClosed>]
[RemovedWith<MbRemovalMultipleAccountMerged>(key: nameof(MbRemovalMultipleAccountMerged.SourceAccountId))]
public record MbRemovalMultipleAccount(
[Key]
Guid Id,
[SetFrom<MbRemovalMultipleAccountOpened>(nameof(MbRemovalMultipleAccountOpened.Name))]
string Name);

View C# snippet source on GitHub

Here, the account-closed event removes the account identified by its event source ID. The account-merged event instead removes the account named by SourceAccountId.

Do not use RemovedWithJoin to delete a root read model. The engine logs a warning and does not subscribe to that removal event; the .NET client also warns when it discovers the declaration. Use RemovedWith with a custom key if another event property identifies the root instance to delete.

For a child collection, the removal declaration can live with the parent property or with the child type.

Apply RemovedWith to the collection property alongside ChildrenFrom when the removal rule belongs to that parent’s use of the child:

[EventType]
public record MbChildrenRemovalPropertyLineItemAdded(Guid ItemId, string Description);
[EventType]
public record MbChildrenRemovalPropertyLineItemRemoved(Guid ItemId);
public record MbChildrenRemovalPropertyOrder(
[Key] Guid Id,
[ChildrenFrom<MbChildrenRemovalPropertyLineItemAdded>(key: nameof(MbChildrenRemovalPropertyLineItemAdded.ItemId))]
[RemovedWith<MbChildrenRemovalPropertyLineItemRemoved>(key: nameof(MbChildrenRemovalPropertyLineItemRemoved.ItemId))]
IEnumerable<MbChildrenRemovalPropertyOrderLine> Lines);
public record MbChildrenRemovalPropertyOrderLine(
[Key] Guid Id,
string Description);

View C# snippet source on GitHub

Apply RemovedWith directly to the child type when every parent using that type should inherit its removal rule:

[EventType]
public record MbChildrenRemovalClassLineItemAdded(Guid ItemId, string Description);
[EventType]
public record MbChildrenRemovalClassLineItemRemoved(Guid OrderId, Guid ItemId);
public record MbChildrenRemovalClassOrder(
[Key] Guid Id,
[ChildrenFrom<MbChildrenRemovalClassLineItemAdded>(key: nameof(MbChildrenRemovalClassLineItemAdded.ItemId))]
IEnumerable<MbChildrenRemovalClassOrderLine> Lines);
[RemovedWith<MbChildrenRemovalClassLineItemRemoved>(
key: nameof(MbChildrenRemovalClassLineItemRemoved.ItemId),
parentKey: nameof(MbChildrenRemovalClassLineItemRemoved.OrderId))]
public record MbChildrenRemovalClassOrderLine(
[Key] Guid Id,
string Description);

View C# snippet source on GitHub

Both examples remove the order line identified by ItemId. The property-level example takes the parent from the event source ID; the class-level example names it explicitly through OrderId.

For child removal, you can specify:

  • key: the event property that identifies the child to remove.
  • parentKey: the event property that identifies the parent; the default is EventSourceId.

Use RemovedWithJoin when the event identifies a child but should remove it from all parents that contain it. Apply the attribute to the collection property or to the child type:

[EventType]
public record MbChildrenRemovedFeatureActivated(Guid FeatureId, string Name);
[EventType]
public record MbChildrenRemovedFeatureDeactivated(Guid FeatureId);
public record MbChildrenRemovedSubscription(
[Key]
Guid SubscriptionId,
[ChildrenFrom<MbChildrenRemovedFeatureActivated>(key: nameof(MbChildrenRemovedFeatureActivated.FeatureId))]
[RemovedWithJoin<MbChildrenRemovedFeatureDeactivated>(key: nameof(MbChildrenRemovedFeatureDeactivated.FeatureId))]
IEnumerable<MbChildrenRemovedFeature> Features);
public record MbChildrenRemovedFeature(
[Key] Guid FeatureId,
string Name);

View C# snippet source on GitHub

In this example, MbChildrenRemovedFeatureDeactivated.FeatureId identifies the child feature. Chronicle removes that feature from every subscription containing it; the event does not need to name one parent.

This shopping-cart projection combines root removal with child removal. Checking out or abandoning the cart removes the whole model; removing an item leaves the cart and its other items intact:

// Events
[EventType]
public record MbRemovalFullShoppingCartCreated(string CustomerName);
[EventType]
public record MbRemovalFullItemAddedToCart(Guid ItemId, string ProductName, decimal Price);
[EventType]
public record MbRemovalFullItemRemovedFromCart(Guid CartId, Guid ItemId);
[EventType]
public record MbRemovalFullCartCheckedOut;
[EventType]
public record MbRemovalFullCartAbandoned;
// Read Models
[RemovedWith<MbRemovalFullCartCheckedOut>]
[RemovedWith<MbRemovalFullCartAbandoned>]
public record MbRemovalFullShoppingCart(
[Key]
Guid Id,
[SetFrom<MbRemovalFullShoppingCartCreated>(nameof(MbRemovalFullShoppingCartCreated.CustomerName))]
string Customer,
[ChildrenFrom<MbRemovalFullItemAddedToCart>(key: nameof(MbRemovalFullItemAddedToCart.ItemId))]
IEnumerable<MbRemovalFullCartItem> Items);
[RemovedWith<MbRemovalFullItemRemovedFromCart>(
key: nameof(MbRemovalFullItemRemovedFromCart.ItemId),
parentKey: nameof(MbRemovalFullItemRemovedFromCart.CartId))]
public record MbRemovalFullCartItem(
[Key] Guid Id,
[SetFrom<MbRemovalFullItemAddedToCart>(nameof(MbRemovalFullItemAddedToCart.ProductName))]
string Product,
[SetFrom<MbRemovalFullItemAddedToCart>(nameof(MbRemovalFullItemAddedToCart.Price))]
decimal Price);

View C# snippet source on GitHub

Check the event’s identifiers before choosing a removal attribute. RemovedWith targets one root model or one parent’s child; RemovedWithJoin targets a matching child across parents. An event coming from another source does not by itself require a join: a custom key can identify a root model directly.

For the collection identity and parent-key rules that removal depends on, continue with Children.