Is there a way to detect and delete "orphaned" Aggregate instances?

I have an application that can have up to 10k documents associated with a single Payment instruction. The Aggregate design is a multi-entity aggregate: Payment-Inst-Aggr ->{Documents-Aggr}.

Payments Instruction and its associated Documents are received in a large JSON file.

The Create command (of the Payment-Inst-Aggr) creates all its Document-Aggr instances. In case of a failure of the Create command after creating a certain number of Document-Aggr instances, the Payment-Inst-Aggr’s create command is repeated. The latter leaves a previous partially created Payment-Inst aggr-> with its Document-Aggr instances, as an “orphaned” aggregate instance. Hence the question above.

Could be a design issue too?

Hi @pals

Let me first look at your design:

When the PaymentInstruction aggregate is created all the associated documents are created as well.
This should be done in the same transaction. You can create the DocumentAggregates from the Payment Instruction aggregate in the CreatePaymentInstructionCommandHandler by using AggregateLifecycle.createNew(). This way the PaymentInstruction aggregate is created only when all documents are created.

Secondly let me try to explain how Aggregates are constructed. When you are not using state stored aggregates and aggregate only exists in-memory and are re-hydrated from the events containing that aggregate identifier. So there is no way to delete these Aggregates because they only exist in memory.
If you change the logic of the Aggregate (by adding variables, command handlers and event sourcing handlers), the state of the aggregate will change also based on old events. So you cannot “detect” of “delete” them but you can change the behavior.

Hope that helps,

Yvonne Ceelie

1 Like

Thankyou for an informative response addressing both of my queries.