4

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?

ShreyansS
  • 167
  • 1
  • 2
  • 10

1 Answers1

12

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
Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • `All nodes, which are mistakenly created thats why became null` means only nodes has been created, I cant see relationship associated with those, either outgoing or incoming. – ShreyansS Jan 10 '14 at 05:32
  • 1
    Maybe you need a different query to find the appropriate nodes. You might try `MATCH (n) WHERE n.name is NULL RETURN n` to check that they're the right ones. – Eve Freeman Jan 10 '14 at 13:28
  • Did you try my query? I have a feeling that `n.name IS NULL` is not what you want to query. – Eve Freeman Jan 10 '14 at 13:34
  • Thanks a lot, Wes. I think, as I m learning to neo4j by my own, so may b lacking to cover important things or may be misleading. Now, need to concentrate and stay tuned with masters like u. – ShreyansS Jan 10 '14 at 13:54
  • # UPDATE 2021 I'm also new to Neo4j, but as I saw in the docs, it says if you want to delete a node/relationship property then you can use `REMOVE` or `type.prop = null` so Neo4j removes the property entirely from the node/rel, maybe the above query is not matching what you want since Neo4j has already removed that property since it doesn't store null valued properties, check it [here](https://neo4j.com/developer/cypher/updating/#_delete_properties) on docs – Hasintha Abeykoon Oct 08 '21 at 03:23