Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete multiple nodes in neo4j

How to delete multiple nodes, (NOT ALL) in neo4j?

I have this query
MATCH (n)
where n.name IS NULL
delete n

It returns more than one node, I want to delete all those nodes(All nodes, which are mistakenly created thats why become null).

The error, I am facing is

javax.transaction.HeuristicRollbackException: Failed to commit transaction Transaction(11, owner:"qtp16626756-84")[STATUS_NO_TRANSACTION,Resources=1], transaction rolled back ---> javax.transaction.xa.XAException

CASE 2: What to do in case of NOT NULL (property) but no any relationship is associated within a node or two; means a node which is kind of orhpan, not connected with other node.

I tried to use LIMIT/SKIP but not working.Any help?

like image 739
ShreyansS Avatar asked Dec 16 '22 02:12

ShreyansS


1 Answers

You need to also delete any relationships connected to those nodes, like so:

match (n)
where n.name IS NULL
optional match (n)-[r]-()
delete n, r

Update for your second case (this deletes only orphans):

match (n)
where NOT (n)--()
  and n.name IS NULL
delete n
like image 95
Eve Freeman Avatar answered Dec 25 '22 23:12

Eve Freeman