3

In relational databases like mysql I was used to do soft delete by setting deleted_at. And then for retrieving SELECT just get rows WHERE deleted_at IS NULL.

I am confused how to implement soft delete in neo4j database. There are multiple ways I am getting but not sure which method or combination will have more advantages.

I also read Neo4j: implementing soft delete with optional relationships but not helping.

  1. Just set property of node deleted_at.
  2. Remove node label and change by prefix "_" like "Student" will become "_Student"
  3. Remove relationship and change by prefix "_" like "TEACHES" will become "_TEACHES"

Which will be best way or combination or other way to achieve soft delete in neo4j?

Community
  • 1
  • 1
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • *Which will be best way* - sounds like it's going to be app-specific. Since there's no direct support for soft-delete, I think this is really an opinion/subjective type of question. Neo4j has a `where` clause allowing you to do the same type of filtering on a `deleted_at` property. There's also nothing stopping you from having an extra label representing soft-deleted nodes, allowing you to filter them out. You probably want to benchmark whichever method you choose (e.g. for the methods you suggested, how well do they perform under load?). – David Makogon Apr 28 '14 at 11:55
  • @DavidMakogon: Yeah. I wanted to know anybody using soft delete. Then which way they are doing? I am new to neo4j. In my case, nodes that will be getting deleted have relationships mostly. If anyone have benchmarks, that will be greate. – Somnath Muluk Apr 28 '14 at 14:11

2 Answers2

4

Which way is the best way will be debatable. If you don't need the actual date of soft delete, then you can just apply/remove a label as necessary:

Mark as "soft deleted":

match (a {name: "foo"}) set a:deleted return a;

Unmark:

match (a {name: "foo"}) remove a:deleted return a;

If you need to assert properties about the soft delete, then it makes sense to model it as a node (e.g. a "soft deletion event") and then link it to the node via a relationship. The nodes that are deleted then are any nodes of a certain type that have a "DELETED" relationship that goes to a soft-delete node. That soft delete node would then have a deleted_at property, along with anything else about the deletion event you're modeling.

I don't think a simple deleted_at property in the node is as good of a solution, because it confuses the node information with the information about the deletion event. I would argue you're trying to model a deletion event here.

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
2

I think have a deleted_at property for node. In which you can store deleted time-stamp.

And at the time of fetching nodes information you can check deleted_at IS NULL. like -

  MATCH (n:node)
  WHERE n.deleted_at IS NULL
  RETURN n;

Storing time-stamp you can get when node is deleted.

You can also have relationship deleted_by between user who deleted the node. So that you can find out who deleted node.

Satish Shinde
  • 2,878
  • 1
  • 24
  • 41