3
votes

I want to delete the the special "root" node in my Neo4j Database.

I already found some Question/Answer in SO, but they are not useful, from different reason.

We uses a db:DATABASE node for store the tenant data in Neo4j, and all other node is somehow connected to this node. So the real Neo4j Root Node just have connections to this DATABASE-s. It means whet a new node is created under the tenant, this new node intermediately has a connection to DATABASE node, and the connection name is CONTAINS.

here is the creation code of the DATABASE node

      CREATE (db:Database { Name: 'TenantName' } ) " )

And I want to delete the whole Tenant, it means I want to delete the whole DATABASE, and all the node which is connected to DATABASE node.

If delete the node with this simple cypher, the node is deleted, but all connected node is still remain in database.

      MATCH (db:Database)
      WHERE db.Name = 'TeanantName'
      DETACH DELETE db

The challenge is: We don't know any Node name or any connection under the DATABASE node.


Already Answered SO questions:

  1. How to delete a node and its connected nodes with Neo4j cypher query? In this answer, we know the name of the relationship. "ACTED_IN", and assumes we have only this connection to deleted node.
  2. Neo4j: How to delete all nodes and relationships beyond a node? In this case, we know the real node structure, but in our case we don't know.
  3. cypher delete node and all the list of related node This assumes that each node in the path is no longer anchored to anything other than the nodes in the path otherwise they will not be able to be removed. Which is not true in our case.
  4. Neo4j how to delete nodes recursively from some start node In this case the whole graph structure is known.

Thank you guys!

1
Your description is rather confusing, I can't quite understand if the :DATABASE node is the root node or if the root node is connected to the :DATABASE node, or how to tell the tenants apart from each other so we don't delete them as well. Can you provide a diagram or output illustrating what you are talking about?InverseFalcon
Sorry, I Edited the questionGyörgy Gulyás

1 Answers

4
votes

Okay, this should be a simple one.

Provided that the :Database node for the tenant and all reachable nodes from it are to be deleted, without regard for node label or relationship type connecting them, the fastest approach will be to use path expander procs from APOC Procedures to match to all nodes in the subgraph and delete them:

MATCH (db:Database)
WHERE db.Name = 'TenantName'
CALL apoc.path.subgraphNodes(db, {}) YIELD node
DETACH DELETE node

If there are a fairly large number of nodes to delete (> 10k or so), then you may want to use apoc.periodic.iterate() to batch the deletes:

CALL apoc.periodic.iterate("
 MATCH (db:Database)
 WHERE db.Name = 'TenantName'
 CALL apoc.path.subgraphNodes(db, {}) YIELD node
 RETURN node",
 "DETACH DELETE node",
 {}) YIELD total, batches, errorMessages
RETURN total, batches, errorMessages