Reputation: 745
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
Upvotes: 6
Views: 11466
Reputation: 3371
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
.
Upvotes: 0
Reputation: 111
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.
Upvotes: 4
Reputation: 61505
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;
Upvotes: 4
Reputation: 25383
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/)
Upvotes: 8
Reputation: 226
For neo4j 2.0 you would do
START n=node(1916)
OPTIONAL MATCH n-[r]-()
DELETE r, n;
Upvotes: 15
Reputation: 4290
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
Upvotes: 11