Live data from Hacker News

Soft deletion probably isn't worth it

brandur.org

201–210 of 514 posts

Re: Soft deletion probably isn't worth it

#201
If you implement soft delete, you should surface it to your user. That's who is accidentally deleting things, and that's who will want to un-delete them. As for side effects like spinning up/down servers, build that into your data model (of course, in a case like Heroku's that can be prohibitively expensive, so don't).

Source: I write back of house software for resale store owners, and accidental deletes happen occasionally. Being able to restore things instills a lot of confidence for our customers.

Re: Soft deletion probably isn't worth it

#203
post #187

I've been a software dev since the 90s and at this point, I've learned to basically do things like audit trails and soft deletion by default, unless there's some reason not to. Somebody always wants to undelete something, or examine it to see why it was deleted, or see who changed something, or blah blah blah. It helps the business, it helps you as developer by giving you debug information as well as helping you to c…

Soft deletion is just one way to achieve undeletion. The author's proposed solution of moving the resource to another table works just as well. You can move it back to the non-deleted table to perform the undeletion. You can keep around these deleted objects as long as you want; they work as a subset of a proper audit trail. The cost of course is you have more tables, but that is less of a cost than having to add "de…

Even more important; the deleted records don't need to live in your cache / RAM / etc. Potentially faster queries.

Re: Soft deletion probably isn't worth it

#204
post #76

Earlier quoted context omitted.

Is not there any attempt to improve the soft deletion at the engine/SQL level? I can see it as a possible feature request.

There's the idea of temporal tables, https://pgxn.org/dist/temporal_tables/ It's not a standard (I think) but it'd let you do a cascading delete and then be able to go and look at the old objects as they were at time of deletion too. You'd need to do things very differently to show a list of deleted objects though.

> temporal tables … It's not a standard

They were introduced in ANSI SQL 2011.

How closely implementations follow the standard I don't know, but something close exists in several DBMSs: I use them regualrly in MS SQL Server, there are plug-ins for postgres, MariaDB has them, and so forth.

Re: Soft deletion probably isn't worth it

#205
post #20

Views are a simple solution to this problem. Pretty much all moderns RDBMSs support updatable views, so creating views over your tables with a simple WHERE deleted_at IS NULL solves the majority of the author's problems, including (IIRC) foreign key issues, assuming the deletes are done appropriately. I feel like a lot of developers underutilize the capabilities of the massively advanced database engines they code ag…

Seriously. That "Downsides: Code leakage" point is nonsensical.

``` CREATE OR REPLACE VIEW active_customer AS SELECT * FROM customer WHERE deleted_at IS NULL OR deleted_at There, I fixed it.

Just use `active_customer` instead of `customer ... deleted_at IS NULL`.

In fact, since the deleted_at column is a timestamp, the original "leakage" query:

``` SELECT * FROM customer WHERE id = @id AND deleted_at IS NULL; ```

is actually broken. A non-null `deleted_at` timestamp that's in the future implies the record hasn't been deleted yet, right?

I've often had junior devs assert that views are some kind of code smell, but these sorts of "canned query/filter that you want to apply very very often" seem like the perfect use case for a view to me. It's DRY, and the fact that your standard "query" is in the database" means you can change it more readily than trying to make sure you hit all the points it might be embedded in the application code.

> I feel like a lot of developers underutilize the capabilities of the massively advanced database engines they code against

Early-ish in the JDBC days a senior dev I was working with at the time (as a junior dev myself) made a pretty good case that "the database is part of the application" that's always stuck with me. Full database independence via software level abstractions is a pretty silly goal outside of library code. If you have a service that makes extensive use of the database, don't throw away the database features in the interest of some abstract "we could swap out oracle with mysql without changing anything" objective. If you want it to be generic, use the SQL standard, but don't be afraid to have a few db-specific bits in the app code if that's a subsystem you might replace once a decade or something.

I blame the DBA/Dev divide for a lot of this. A lot of the impedance between these layers is social/procedural. If you can change the DB as easily as the code, there's a lot less fear of using the right tool for the specific job.

Re: Soft deletion probably isn't worth it

#207
It's interesting that the author notes that, as far as he's aware, nobody's ever undeleted something. It could be true. But I'm wondering if maybe he simply hasn't seen it first-hand since the action of recovering something is often handled by a customer-facing team and not by a developer.

Re: Soft deletion probably isn't worth it

#208
post #20

Views are a simple solution to this problem. Pretty much all moderns RDBMSs support updatable views, so creating views over your tables with a simple WHERE deleted_at IS NULL solves the majority of the author's problems, including (IIRC) foreign key issues, assuming the deletes are done appropriately. I feel like a lot of developers underutilize the capabilities of the massively advanced database engines they code ag…

Views are such a powerful concept I’m honestly disheartened by how hard it is to use, replicate or leverage that functionality outside of dropping straight into the db shell

See http://materialize.com

Re: Soft deletion probably isn't worth it

#209
I use soft deletes in our system and literally used it to restore an accidentally deleted item about 3 hours ago. Took a second to toggle the deleted item.

I don't get how this rocket science. Almost every query in the system is some kind of where clause on a fk to account or user or project or some other critical object ... so there are only a few places in the ORM where I need to support this.

Re: Soft deletion probably isn't worth it

#210
post #20

Views are a simple solution to this problem. Pretty much all moderns RDBMSs support updatable views, so creating views over your tables with a simple WHERE deleted_at IS NULL solves the majority of the author's problems, including (IIRC) foreign key issues, assuming the deletes are done appropriately. I feel like a lot of developers underutilize the capabilities of the massively advanced database engines they code ag…

I was going to chime in with this. thanks. One issue with views however is that a lot of these features require more and more nuanced knowledge of RDBMSes where these days unless you have a veteran architect, most of the team just knows the various library/tooling that interacts with "a variety of databases" so there is often less effort to go deeper.

Where is this anti-fb culture? Is it a startup thing?

Everywhere I have worked people know a decent amount about their data store. Not architects, just mid devs and higher.

Post reply on HN