Add callback to `Repository` to allow erasure of entities
We need to provide an ability to permanently erase entities. When such a feature would be useful?
-
Remove data “noise” for entities with short lifecycle. Consider a Process Manager, which acts only once and when finished is of no interest. We keep the history of all the related events, why keep the state of the Process Manager?
-
Prevent performance degradation because of old data. Keeping the event stream as the source of truth is enough. We can delete aggregates, projections, etc. that are out of interest for a business because the data is old. Who would be interested in a pizza purchase after 3 years from now? There are legal requirements related to bookkeeping, but once they are met, the data can be erased.
Cleaning event stream from old data is a separate and complex issue, and as such should be addressed separately.
Implementation considerations
If an entity implements EntityWithLifecycle its modification transaction may result in the deleted flag set to true. By default, a Repository should do nothing about it. But the user should be able to override a method which may look like this:
protected void onDeleted(I entityId) { ... }
To preform the erasure from the storage.
Probably, it may be a good idea to have a separate callback for archive flag. Smth like onArchive.
An example use case: forcefully create a snapshot when an entity is archived.
A related migration API has been added #1241.
It is possible to delete Projections and ProcessManagers forever in scope of data migration. While it's not the resolution for this issue, but it's for sure related.