2

I’ve worked with database schemas that often had a soft delete field indicator in tables (such as deleted_at, invalidated, and so on) to keep track of the deletion status of records.

It’s obvious that they offer some benefits, for example the ability to revert a soft deleted record by just switching or nulling a field.

Are soft deletes worth it?

Some of the problems I’ve encountered when introducing soft deletions in my own database architecture:

  1. Composite Keys

    Suppose a record with a composite primary key of (field1, field2).

    If that record is soft deleted, and then another one is created with the same composite key, there would be a conflict.

    Solutions:

    1. Create a surrogate key. But more often than not they would just make queries and joins unnecessarily difficult, especially in applications where data is modified often.

    2. Delete the previous record. But that would be considered a side effect rather than a good practice, in addition to losing the audit trail benefit.

    3. Drop the primary key and just put a unique constraint where indicator = NULL. Seems good, but I don't see what am I am losing by replacing a PRIMARY KEY constraint with a UNIQUE one.

  2. Workarounds and Impact on Code

    Since more often than not databases are used alongside some sort of application, there could be implications on the code.

    Some ORMs support soft deletions out of the box by modifying queries, but it’s not guaranteed (e.g., Hibernate recently added out-of-the-box support for soft deletions). This would mean that developers would have to write their own queries, making sure that the indicator is always checked when needed.

  3. Performance Implications

    As the database grows, deleted records will grow. Having 20% to 40% deleted records in a table would be unnecessary weight for the database. Indexes would need to exclude deleted records.

    A solution could be to delete extremely old records with a scheduled job.

The benefits of soft deletions might not be worth it and better solutions for deletion recovery and auditing may exist (e.g., just having a deleted records table).

Another edge case is cascade soft deletion. While on standard deletions we have ON CASCADE, soft deletions might need more sophisticated approaches, such as triggers and functions that mark the children as soft deleted.

6
  • 5
    "Worth it" valuations are subjective and cannot be universally answered. You have to decide your own priorities between cost, performance, data reliability and risk of data loss. Commented Dec 3 at 4:21
  • 6
    Sorry, but there is no way around a proper requirement analysis for the specific data in stake - always, always, always! Either there is a requirement to keep deleted entities online accessible somewhere in the live database, or there is no such requirement. This may differ from entity to entity, and depends on the specific use cases for which the data is kept. You are looking for a general approach where there is none. Commented Dec 3 at 6:38
  • 3
    Soft deletion in the way you present it is not worth it IMO. For audit and/or rollback having a simple flag is not enough. You need a history of changes. And if you have history, then you don't need flags. And you can treat concrete tables as a current snapshot of the history. Commented Dec 3 at 7:17
  • That being said, sometimes it is just easier to do flagged soft deletions. Instead of setting up entire history table. But I wouldn't recommend it Commented Dec 3 at 7:20
  • 2
    Something else to consider when entertaining "soft deletes" is if deleting something is really the business intention. Often times, something should be "invalid" or "closed". Anytime I encounter a need for soft deletes, I question the underlying business processes - not that those processes are wrong, but that those processes are not actually a "delete"; it's something else better represented by a more intentional design. Commented Dec 3 at 13:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.