6
votes

I'm playing with 2.0 M6 neo4j server (oracle jdk7 on win7 64).

I'm trying to delete a node and its relationships using a single cypher query over the REST API.

The query I create (which works if I run it in the browser UI) looks like:

START n = node( 1916 ) MATCH n-[r]-() DELETE n, r

Which by the time I put it through gson comes out as:

{"query":"START n \u003d node( 1916 ) MATCH n-[r]-() DELETE n, r"}

Which when sent to the server gets the response:

{
  "columns" : [ ],
  "data" : [ ]
}

My test fails because the node can still be found in neo4j server by its id...

If I simplify my query to just delete a node (that has no relationships) so its:

START n = node( 1920 )  DELETE n

Which becomes

{"query":"START n \u003d node( 1920 )  DELETE n"}

Then the node is deleted.

Have I missed something?

Thanks, Andy

6

6 Answers

11
votes

MATCH n-[r]-() will only match the node if there is at least one relationship attached to it.

You want to make the relationship match optional: MATCH n-[r?]-()

Also, you need to delete the relationships before the node.

So, your full query is:

START n=node(1916)
MATCH n-[r?]-()
DELETE r, n
15
votes

For neo4j 2.0 you would do

START n=node(1916)
OPTIONAL MATCH n-[r]-()
DELETE r, n;
8
votes

Both START and the [r?] syntax are being phased out. It's also usually not advised to directly use internal ids. Try something like:

match (n{some_field:"some_val"}) optional match (n)-[r]-() delete n,r

(see http://docs.neo4j.org/refcard/2.1/)

4
votes

The question mark(?) is not supported in Neo4J 2.0.3, so the answer would be to use OPTIONAL MATCH

START n=node(nodeid) OPTIONAL MATCH n-[r]-() DELETE r, n;

4
votes

And again there is a pretty syntax change. Neo4j 2.3 introduces the following:

MATCH (n {id: 1916})
DETACH DELETE n

Detach automatically deletes all in- and outgoing relationships.

0
votes

Based upon latest documents and I have also tested it

START n=node(1578)
MATCH (n)-[r]-()
DELETE n,r

We have to put () around n and there is also no need of ? in [r?].

Even it works without OPTIONAL.