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
For neo4j 2.0 you would do
START n=node(1916)
OPTIONAL MATCH n-[r]-()
DELETE r, n;
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
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/)
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;
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.
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.
Related
I want to delete all nodes of a given type and their relations. In total there are 1.4 million nodes of this type.
Using MATCH (n:Type) DETACH DELETE n Neo4j hangs itself up after a few minutes and has to be restarted.
Is there a better way to delete a large number of nodes? Can I delete them in chunks somehow (LIMIT is not supported with DELETE)?
Try this
Match (n:Type) with n
Match (n)-[r]-()
Delete n, r
If you want to delete them in chunks the query would look like
Match (n:Type) with n limit 1000
Match (n)-[r]-()
Delete n, r
I have modified the 'vanilla' initial query in this console, and added one relationship type 'LOCKED' between the 'Morpheus' and 'Cypher' nodes.
How can I modify the existing (first-run) query, which is a variable length path so that it no longer reaches the Agent Smith node due to the additional Locked relationship I've added?
First-run query:
MATCH (n:Crew)-[r:KNOWS|LOVES*2..4]->m
WHERE n.name='Neo'
RETURN n AS Neo,r,m
I have tried this kind of thing:
MATCH p=(n:Crew)-[r:KNOWS|LOVES*2..4]->m
WHERE n.name='Neo'
AND none(rel IN rels(p) WHERE EXISTS (StartNode(rel)-[:LOCKED]->EndNode(rel)))
RETURN n AS Neo,r,m
..but it doesn't recognize the pattern inside the none() function.
I'm using Community 2.2.1
Thanks for reading
I'm pretty sure you can't use a function in a MATCHy type clause like that (though it's clever). What about this?
MATCH path=(neo:Crew)-[r:KNOWS|LOVES|LOCKED*2..4]->m
WHERE neo.name='Neo'
AND NOT('LOCKED' IN rels(path))
RETURN neo,r,m
EDIT:
Oops, looks like Dave might have beat me to the punch. Here's the solution I came up with anyway ;)
MATCH p=(neo:Crew)-[r:KNOWS|LOVES*2..4]->m
WHERE neo.name='Neo'
WITH p, neo, m
UNWIND rels(p) AS rel
MATCH (a)-[rel]->(b)
OPTIONAL MATCH a-[locked_rel:LOCKED]->b
WITH neo, m, collect(locked_rel) AS locked_rels
WHERE none(locked_rel IN locked_rels WHERE ()-[locked_rel]->())
RETURN neo, m
Ok, this is a little convoluted but i think it works. The approach is to take all of the paths and find the last known good nodes (ones that have LOCKED relationships leaving them). Then use that node(s) as a new ending point(s) and return the paths.
match p=(n:Crew)-[r:KNOWS|LOVES|LOCKED*2..4]->m
where n.name='Neo'
with n, relationships(p) as rels
unwind rels as r
with n
, case
when type(r) = 'LOCKED' then startNode(r)
else null
end as last_good_node
with n
, (collect( distinct last_good_node)) as last_good_nodes
unwind last_good_nodes as g
match p=n-[r:KNOWS|LOVES*]->g
return p
I think this would be simpler if there was a locked: true property on the KNOWS and LOVES relationships.
I am new to Neo4j // Cypher and I am following the tutorial.
Playing with the movie database i tried to delete all the "ACTED_IN" relationship using the following query
match (:Person)-[r:ACTED_IN]->(:Movie)
DELETE r;
however i found that i have i still have some "ACTED_IN" relations between nodes and i have to re-run the previous query several times to completely remove those relations.
why is it not working as i expected?
what is the right way to do this?
thanks
Just tried, it worked for me (Using Neo4j 2.0.1 and 2.1.0-M01)
match (:Person)-[:ACTED_IN]->(:Movie) return count(*);
-> count(*)
172
match (:Person)-[r:ACTED_IN]->(:Movie) delete r;
-> Deleted 172 relationships, returned 0 rows in 172 ms
match (:Person)-[:ACTED_IN]->(:Movie) return count(*);
-> count(*)
0
To delete all 'follows' relations associated with node with name 'vishal1#myemail.com' :
MATCH (n { name:'vishal1#myemail.com' })-[r:follows]->() DELETE r
Worked for me :)
Further, you can also specify label(in my case 'User') to distinguish between nodes with same name
MATCH (n:User { name:'vishal1#myemail.com' })-[r:follows]->() DELETE r
Neo4j Relationshiptypes are case sensitive. So ACTED_IN and acted_in will act as different relationshipTypes
I know this question is asked by many people already
for my research, here's some questions asked before
How to delete all relationships in neo4j graph?
https://groups.google.com/forum/#!topic/neo4j/lgIaESPgUgE
But after all, still can't solve our problems,
we just want to delete "ALL" nodes and "ALL" relationships
suppose delete "ALL" can see there are left 0 nodes 0 properties and 0 relationships
This is the screenshot i took after executing the delete "ALL" suggested by forum
My question still the same, how do delete all nodes and all relationships in neo4j
As of 2.3.0 and up to 3.3.0
MATCH (n)
DETACH DELETE n
Docs
Pre 2.3.0
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r
Docs
you are probably doing it correct, only the dashboard shows just the higher ID taken, and thus the number of "active" nodes, relationships, although there are none. it is just informative.
to be sure you have an empty graph, run this command:
START n=node(*) return count(n);
START r=rel(*) return count(r);
if both give you 0, your deletion was succesfull.
for a big database you should either remove the database from the disk (after you stop the engine first I guess) or use in Cypher something like:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
WITH n,r LIMIT 50000
DELETE n,r
RETURN count(n) as deletedNodesCount
see https://zoomicon.wordpress.com/2015/04/18/howto-delete-all-nodes-and-relationships-from-neo4j-graph-database/ for some more info I've gathered on this from various answers
Neo4j cannot delete nodes that have a relation. You have to delete the relations before you can delete the nodes.
But, it is simple way to delete "ALL" nodes and "ALL" relationships with a simple chyper.
This is the code:
MATCH (n) DETACH DELETE n
DETACH DELETE will remove all of the nodes and relations by Match
if the name of node is for example : abcd then below query will work :
MATCH (n:abcd)
DETACH DELETE n
This will only delete the node with label "abcd" and all its relation-ships.
Probably you will want to delete Constraints and Indexes
It will do the trick..
Match (n)-[r]-()
Delete n,r;
Using Cypher how can I get all nodes in a graph? I am running some testing against the graph and I have some nodes without relationships so am having trouble crafting a query.
The reason I want to get them all is that I want to delete all the nodes in the graph at the start of every test.
So, this gives you all nodes:
MATCH (n)
RETURN n;
If you want to delete everything from a graph, you can do something like this:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n, r;
Updated for 2.0+
Edit:
Now in 2.3 they have DETACH DELETE, so you can do something like:
MATCH (n)
DETACH DELETE n;
Would this work for you?
START a=node:index_name('*:*')
Assuming you have an index with these orphaned nodes in them.
This just works fine in 2.0:
MATCH n RETURN n
If you need to delete some large number of objects from the graph, one needs to be mindful of the not building up such a large single transaction such that a Java OUT OF HEAP Error will be encountered.
If your nodes have more than 100 relationships per node ((100+1)*10k=>1010k deletes) reduce the batch size or see the recommendations at the bottom.
With 4.4 and newer versions you can utilize the CALL {} IN TRANSACTIONS syntax.
MATCH (n:Foo) where n.foo='bar'
CALL { WITH n
DETACH DELETE n
} IN TRANSACTIONS OF 10000 ROWS;
With 3.x forward and using APOC
call apoc.periodic.iterate("MATCH (n:Foo) where n.foo='bar' return id(n) as id", "MATCH (n) WHERE id(n) = id DETACH DELETE n", {batchSize:10000})
yield batches, total return batches, total
For best practices around deleting huge data in neo4j, follow these guidelines.