Remove node and relationships using cypher request by REST API

I play with server 2.0 M6 neo4j (oracle jdk7 on win7 64).

I am trying to remove a node and its relationships using a single cypher request on a REST API.

The generated request (which works if I run it in the browser interface) looks like this:

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

Which by the time I passed it through gson, is issued as:

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

That when sending to the server receives a response:

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

My test failed because node can still be found on the neo4j server by its id ...

If I simplify my query, I just remove the node (which has no relationship), so its:

 START n = node( 1920 ) DELETE n 

Which becomes

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

Then node is deleted.

Did I miss something?

Thanks Andy

+6
source share
6 answers

MATCH n-[r]-() will correspond only to node if at least one relation is attached to it.

You want the ratio to be optional : MATCH n-[r?]-()

In addition, you need to remove the relationship to node.

So your complete request:

 START n=node(1916) MATCH n-[r?]-() DELETE r, n 
+11
source

For neo4j 2.0 you would do

 START n=node(1916) OPTIONAL MATCH n-[r]-() DELETE r, n; 
+15
source

The syntax START and [r?] gradually terminated. It is also generally not recommended to directly use internal identifiers. Try something like:

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

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

+8
source

Question mark (?) Is not supported in Neo4J 2.0.3, so the answer will be to use OPTIONAL MATCH

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

+4
source

And again a pretty syntactic change appears. Neo4j 2.3 introduces the following:

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

Disconnect automatically deletes all inbound and outbound relationships.

+4
source

Based on the latest docs, I also tested it

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

Should we put () around n, and also not necessary? in [r?].

Even it works without OPTIONAL .

0
source

Source: https://habr.com/ru/post/956836/


All Articles