I'm trying to execute following query:
MATCH (movie:Movie {title:"test"})-[r]-() DELETE movie, r
to delete a :Movie node and all its relationships. It's all good, except if the query doesn't have any relationships, it fails to MATCH the movie. I've tried with OPTIONAL MATCH, but no luck.
I'm looking for a way to DELETE a movie node no matter if it has or doesn't have any relationships, but if it has, to DELETE them as well.
In new Neo4j versions (since 2.3 I think) you can use such syntax:
MATCH (movie:Movie {title:"test"})
DETACH DELETE movie
There's OPTIONAL MATCH:
MATCH (movie:Movie {title:"test"})
OPTIONAL MATCH (movie)-[r]-()
DELETE movie, r
The best option to do this today (Dec 2021) is:
MATCH (movie:Movie {title:"test"}) DETACH DELETE movie
See this: https://www.quackit.com/neo4j/tutorial/neo4j_delete_a_relationship_using_cypher.cfm
Related
With the following graph:
(Boxer)-[:STARTS]->(Round)-[:CONTINUES]->(Round)-[:CONTINUES]->(Round)-[:CONTINUES]->(Round)
I want to remove a (Round) in the linked list.
I got a successful result by doing this:
MATCH (round:Round {uuid: $round.uuid})
MATCH (prevRound)-[:CONTINUES]->(round)-[:CONTINUES]->(nextRound)
DETACH DELETE round
MERGE (prevRound)-[:CONTINUES]->(round)
But this will work for any Round except the first one, because it has a STARTS a relationship. So I tried this:
MATCH (round:Round {uuid: $round.uuid})
MATCH (prevRound)-[prevRel:CONTINUES|STARTS]->(round)-[nextRel:CONTINUES]->(nextRound)
DETACH DELETE round
MERGE (prevRound)-[prevRel]->(round)
But I get this error:
Neo4jError: Variable `prevRel` already declared
MERGE (prevRound)-[prevRel]->(nextRound)"
You cannot use a identifier to dynamically create a relationship in cypher.
In your statement the planner thinks that you are trying to use prevRel as an identifier in your MERGE but it is already used in the MATCH above.
Fortunately there is a solution for this using APOC. The apoc.merge.relationship procudeure can be used to create a new relationship type that is the same as the one you removed when you removed the round.
MATCH (round:Round {uuid: $round.uuid})
MATCH (prevRound)-[prevRel:CONTINUES|STARTS]->(round)-[nextRel:CONTINUES]->(nextRound)
DETACH DELETE round
WITH prevRound, prevRel, nextRound
CALL apoc.merge.relationship(prevRound, type(prevRel), {}, {}, nextRound) YIELD rel
RETURN prevRound, rel, nextRound
When fetching or when I try to delete a specifc node like
MATCH (p)
where ID(p)=79259223
OPTIONAL MATCH (p)-[r]-()
//drops p's relations
DELETE r,p
I get the following error
While loading relationships for Node[79259223] a Relationship[87331456] was encountered that had startNode: 80312215 and endNode: 83719851, i.e. which had neither start nor end node as the node we're loading relationships for
I also run the ConsistencyChecker what resulted in a big list of inconsistencys. However how can you fix these inconsistencys? I can not delete the nodes for instance
Here is a possible way to "fix" an occurrence of this error. Unfortunately, it is a fairly manual approach that has to be used for every node that encounters the same problem.
Before you delete the node, you can try to delete the inconsistent relationship by its native neo4j ID. For example:
MATCH ()-[r]->()
WHERE ID(r) = 87331456
DELETE r;
NOTE: Before deleting that relationship, you should first try to take a look at it (e.g., replace DELETE WITH RETURN) to understand what you are planning to delete. You may want to do something else first or instead.
If that deletion works, then try to delete the node again, as follows:
MATCH (p)
WHERE ID(p) = 79259223
DETACH DELETE p;
Notice that I use the DETACH DELETE syntax, which will attempt to delete all the relationships for the specified node.
I recently upgraded my Neo4j database to v. 3 (3.0.6). Since then I am having trouble when trying to delete nodes with Cypher. This is a new problem since the upgrade. They Cypher query is:
MATCH (p) WHERE id(p) = 83624
OPTIONAL MATCH (p)-[r]-(n)
OPTIONAL MATCH (p)-[r2]-(n2)
WHERE NOT ('Entity' in labels(n2))
DELETE r, r2, p, n2
This now results in the error Unable to load NODE with id 83624
The exact query with RETURN instead of DELETE returns the node. I've also tried swapping out DELETE with DETACH DELETE of the nodes, but that gives me the same error.
It looks like this question has been asked before but without a solution. Any idea what is causing this error?
I'm a little confused by this query. Both of your optional matches are identical, in that they will match to any relationship to any node connected to p.
Let me make sure I understand what you're trying to do: Find a node by ID, then delete that node along with any connected node that is not an Entity (as well as the relationships connecting them). Is that right?
If so, this query might work better:
MATCH (p) WHERE id(p) = 83624
OPTIONAL MATCH (p)--(n)
WHERE NOT n:Entity
DETACH DELETE n
DETACH DELETE p
Detaching a node deletes all relationships connected to that node, and you can only delete a node that has zero relationships.
Also, just to note, it's not a good idea to use the internal ids for uniquely identifying nodes. It's recommended to use your own unique IDs instead, and create unique constraints on that property for that label.
In SQL:
Delete From Person Where ID = 1;
In Cypher, what's the script to delete a node by ID?
(Edited: ID = Neo4j's internal Node ID)
Assuming you're referring to Neo4j's internal node id:
MATCH (p:Person) where ID(p)=1
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
If you're referring to your own property 'id' on the node:
MATCH (p:Person {id:1})
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
The cleanest sweep for a node with id "x" is
MATCH (n) where id(n) = x
DETACH DELETE n
https://neo4j.com/docs/cypher-manual/current/clauses/delete/#delete-delete-a-node-with-all-its-relationships
https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-id
Old question and answered, but to delete node when it has relationships, use DETACH
MATCH (n) where ID(n)=<your_id>
DETACH DELETE n
or otherwise you get this:
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<21>, because it still has relationships. To delete this node, you must first delete its relationships.
It's like SQL's CASCADE
When the node is a orphan.
Start n=node(1)
Delete n;
Following the link provided by #saad-khan, here's an example for getting the nodes and relationships ids.
The code below shows the ids, so you can make sure that you're deleting everything related to the given ID.
MATCH (node)-[relation:HAS]->(value)
where ID(node)=1234
RETURN ID(instance), ID(value), ID(r)
Ps.: ":HAS" is an example of an relationship.
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;