Deleting events based on persistence ID
truongio opened this issue · comments
Hi,
Is there any way to delete events through an API call if I have a persistence ID?
Yes and no, you can check that part of the akka persistence plugin docs:
https://doc.akka.io/docs/akka/current/persistence.html#message-deletion
You can call that method with the right seqNr and it will delete it. The current seqNr you can find on the PersistenceActor itself. There is a method for that.
However, you need to take a few things into consideration.
- this can only be done from inside the PersistentActor you want to delete. You need to load it, send a message to it that triggers the method
- by default, the jdbc plugin is configured to use logical deletes, you need to change it. See https://github.com/dnvriend/akka-persistence-jdbc/blob/master/src/main/resources/reference.conf#L17-L26
- the last event can't be hard-deleted. When using hard deletes, it will delete all but the last. You may want to add a
Deleted
event to signal this. At the end, you will have one single row for that persistent actor containing theDeleted
event all the others will be deleted.
@renatocaval I'm sorry, I forgot that I use Akka Typed.
The same holds. Except that you need to call the equivalente delete message in Akka Typed
The same holds. Except that you need to call the equivalente delete message in Akka Typed
@octonato are you sure? I'm not able to find such a method in the Typed version. I can only set retentionCriteria, which is not the same as deleting all events.
@aludwiko, you can delete the events after making a snapshot as described here https://doc.akka.io/docs/akka/current/typed/persistence-snapshot.html#event-deletion
There is no functionality to delete all events directly though. Maybe that's what you are looking for?
The deletion in typed is done in a controlled way, meaning, if a snapshot is made then you can delete all events previous to the snapshot.
If you want to completely delete all events, then I guess the options are:
- delete direct on the db (all or where persistence_id = ?)
- use the existing snapshot-retention with event deletion as linked here
There is one thing missing in the typed API that would help you here. This is described here akka/akka#29685
In a nutshell, it will be cool to trigger events deletion based on a snapshot predicate. Then you could have a state, eg: Deleted
. It would work as a tombstone. Once you move your model to Deleted
, it refuses all new commands and with a snapshot predicate with event deletion, you could say: "make a snapshot and delete all events behind it".
From that point in time, your journal is cleared, the state will be Deleted
and no new events will be ever persisted.
Of course, that's the example of using a tombstone. Doesn't mean that you couldn't revert to a Reset
state for instance.