How to remove/clean-up state of EventSourcedBehavior when task done?

I have some persistent actors (implementing EventSourcedBehavior) that manage certain jobs/tasks. When a job is completed the EventSourceBehavior that was managing it can be stopped and its state (events/snapshot) is not needed anymore. I would like to be able to remove the state when stopping the actor. Something like an Effect().deleteStateAndStop().

Is there a concept for this already? Or any other approach to avoid orphaned events/snapshots piling up?

Hi Peter,

There’s nothing to do what you ask for out of the box, at least to my knowledge. You’ll have to “manually” delete the event records you no longer need from the database.

Thank you, @chmodas. The old PersistentActor at least had one deleteSnapshots method that we were using:

deleteSnapshots(SnapshotSelectionCriteria.latest());

I don’t really like to dig into the implementation details of the journal and snapshot plug-in implementations to avoid piling up orphaned event and snapshot data. But I will consider this now or maybe use a different approach than persistent actor in the first place. Will think about this…

Hey Peter,

You can still delete events by the snapshot. And you could delete the snapshot (same documentation).

Perhaps a combination of the two would be sufficient for your case? You can end up leaving a tombstone of sorts.

Hi @chmodas, yes, this is great. I was using the RetentionCriteria already with .withDeleteEventsOnSnapshot(). And I could use a tombstone event to trigger a snapshot at the end. But this would still leave behind the last snapshot. I think this is the only part I could not figure out myself: How to delete that last snapshot?

Hey Peter,

You are right, it will leave the last snapshot. The Effect API does not provide you with any means to erase all snapshots. However, you can still achieve what you desire using the Journal and Snapshot plugin APIs, if you don’t want to touch the data directly.

Perhaps you might have an actor tasked with deleting those entities’ entries using the def asyncDeleteMessagesTo(persistenceId: String, toSequenceNr: Long) and def deleteAsync(persistenceId: String, criteria: SnapshotSelectionCriteria) to remove the events and snapshots, respectively? Something along those lines.

Borislav