Reputation: 1598
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:
Thank you guys!
Upvotes: 4
Views: 935
Reputation: 30417
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
Upvotes: 4