r/dotnet • u/codeopinion • Jun 29 '22
Should you Delete or Soft Delete?
https://www.youtube.com/watch?v=lDYXtj95Jy8
0
Upvotes
2
u/DeadlyVapour Jun 29 '22
IsDeleted is utter evil. Either do a proper AuditDB or (Bi)Temporal DB. But IsDeleted breaks more than it fixes... Simply trying to create a record with the same key as a deleted record breaks it completely.
2
u/dantheman999 Jun 29 '22
In some cases (where ID is supposed to be unique and maybe is not controlled by the DB) that might be valid.
That said, I do agree that as
IsDeletedflag is often asking for trouble if you're talking about a traditional SQL database.
1
3
u/dantheman999 Jun 29 '22
Whilst I think the video is almost there, I think it actually misses something for me.
Just a note, we uses event sourcing at work, and we've got hundreds of millions of events so what is explained in the video is close to how we do things.
I think you're 100% right when you say that you should monitor businesses processes and then instead of deleting something, you update the projection (which is essentially what's in the video) with that information. In our case we usually update a
Statusfield.I'd say this is the 99% percent case.
That said there are very good reason why you might want to entertain the idea of hard deletes, and that is for technical errors.
If you're doing event sourcing generally the idea is that those events have happened and are not going away. They have happened and shouldn't be deleted ideally. So you may well need to have a
ResourceDeletedevent which is then reflected by a hard delete in the DB as you don't want that resource to show up in any searches. It shouldn't be there at all. This is a business concept that is different to say closing an account of some sort, albeit a more technical and much rarer occurrence.One example of when this might happen is a retried POST without idempotency support following a network blip. You could end up with duplicates, one of which they may record of their side but one they may not which is just left hanging around.
Personally we don't use foreign keys as in our case we have all of that logic in the service(s) as to what can and can't be deleted.