Reputation: 47
While using the DELETE
clause to delete a vertex in age. For example the following query
SELECT * FROM cypher('graph_name', $$
MATCH (v:Useless)
DELETE v $$) as (v agtype);
Matches the vertex with the label Useless
and thus deleting the vertex that matches such attribute. My question is what would be the behavior of the DELETE
clause if the MATCH
clause returns multiple vertices? Does it delete all matched vertices or it deletes the first match?
I know the DETACH DELETE
deletes the match and their edges what would be the behavior too if it returns multiple vertices and edges.
Upvotes: 1
Views: 190
Reputation: 584
DELETE will delete everything that matches that label, so if you want to delete only some of them you can use the WHERE clause to filter the nodes.
SELECT * FROM cypher('graph_name', $$
MATCH (v:Useless) WHERE v.property = value
DELETE v $$) as (v agtype);
Use this documentation for use of MATCH and WHERE.
DETACH DELETE
will also do the same, and delete all the matching nodes and associated edges.
Upvotes: 0
Reputation: 15
In a Cypher query, when multiple vertices are returned by MATCH clause then all matched vertices will be deleted by the DELETE clause.
As far as your question is concerned, answer is yes, executing the DELETE
clause will indeed delete all the vertices that were matched in the query.
Upvotes: 0
Reputation: 301
In addition to other answers, I will use AGE Viewer to demonstrate how the DELETE
clause works:
First, I created three vertices:
SELECT *
FROM cypher('graph_name', $$
CREATE (:Useless {v:1}), (:Useless {v:2}), (:Useless {v:3})
$$) as (v agtype);
If I run the DELETE
clause for all vertices with the label Useless
, it will delete all vertices:
SELECT * FROM cypher('graph_name', $$
MATCH (v:Useless)
DELETE v $$) as (v agtype);
In the following image, the query return all vertices. It says "Displaying 0 nodes" because all vertex were deleted:
So, the DELETE
clause deletes all matched vertices.
Upvotes: 0
Reputation: 1
DELETE will delete all the matching vertices regardless of their properties and its always recommended to use delete with some sort of criterion unless you need to sweep all the vertices with the particular label. Further to be more safe and fault tolerant DETACH DELETE should be used so that the edges on the delete node not be left hanging aimlessly.
Upvotes: 0
Reputation: 268
Yes, it will delete all matched vertices with that label.
To filter it out or be more specific, you should use the WHERE
clause. Check out this answer from another question.
Upvotes: 0
Reputation: 138
Apparently there are 2 possible ways this query can go depending on your graph, I have discovered this after experimentation.
1.) If your graph only contains isolated vertices with the label useless, all of them will be deleted.
2.) If your graph contains any (even a single one) non-isolated vertex with this label, no vertices at all will be deleted.
Let's confirm this behavior: -
We create 3 isolated vertices with the label Useless, numbered 1, 2, and 3.
test=# SELECT * FROM cypher('test', $$
CREATE (:Useless {number:1}), (:Useless {number:2}), (:Useless {number:3}) $$) as (type agtype);
type
------
(0 rows)
They are deleted just fine.
test=# SELECT * FROM cypher('test', $$
MATCH(n:Useless) delete n
$$) as (type agtype);
type
------
(0 rows)
Next we create 4 vertices with the same label with an edge between 2 of them.
test=# SELECT * FROM cypher('test', $$
CREATE (:Useless {number:1}), (:Useless {number:2}), (:Useless {number:3})-[:UselessStill]->(:Useless {number:4})
$$) as (type agtype);
type
------
(0 rows)
Trying to delete them: -
test=# SELECT * FROM cypher('test', $$
MATCH(n) DELETE n
$$) as (type agtype);
2023-05-13 12:02:10.802 CEST [22412] ERROR: Cannot delete vertex n, because it still has edges attached. To delete this vertex, you must first delete the attached edges.
2023-05-13 12:02:10.802 CEST [22412] STATEMENT: SELECT * FROM cypher('test', $$
MATCH(n) DELETE n
$$) as (type agtype);
ERROR: Cannot delete vertex n, because it still has edges attached. To delete this vertex, you must first delete the attached edges.
How do we know no vertices were deleted? Let's query: -
MATCH(n) RETURN n
$$) as (type agtype);
type
-----------------------------------------------------------------------------------
{"id": 6755399441055745, "label": "Useless", "properties": {"number": 1}}::vertex
{"id": 6755399441055746, "label": "Useless", "properties": {"number": 2}}::vertex
{"id": 6755399441055747, "label": "Useless", "properties": {"number": 3}}::vertex
{"id": 6755399441055748, "label": "Useless", "properties": {"number": 4}}::vertex
(4 rows)
I just checked in neo4j and it in indeed the desired behavior, since neo4j works the same way.
Adding 4 vertices with 1 edge in between.
Attempting a delete
Still the same
Upvotes: 0
Reputation: 217
If match clause in a cypher query returns multiple vertices then the delete clause will delete all of matched vertices. The DELETE is applied to all vertices that are returned by MATCH clause.
MATCH (u:Useless)
DELETE v
If it return multiple edges then the delete clause will detach delete all of the matching edges.
In answer to your question yes it will delete all of matched vertices
Upvotes: 0
Reputation: 151
I will explain all ways by which you can do the delete thing in Apache Age.
For deleting a Vertex:
This query will delete all the vertexes named useless.
select *
from cypher('graph_name',$$
Match (v:Useless)
Delete v
$$) as (v agtype);
For deleting all Vertices and edges:
This query will delete all the edges associated with the vertex first and then delete the edges.
select *
from cypher('graph_name',$$
Match (v:Useless)
DETACH Delete v
$$) as (v agtype);
For deleting edges only:
This query will delete "knows relationship" edges associated to these Andres named vertex.
select *
from cypher('graph_name',$$
Match (n{name:'Andres'})-[r:KNOWS] ->()
Delete r
$$) as (v agtype);
In all the above queries, nothing will be returned after deleting the vertex or edge.
For returning a deleted Vertex:
This query will delete all the vertexes named useless.
select *
from cypher('graph_name',$$
Match (n{name:'Andres'})
Delete n
Return n
$$) as (v agtype);
This above query will return the deleted vertex, we can amend this query to delete the relationship too and return that too.
Upvotes: 0
Reputation: 135
If you want to be more specific, you can use the MATCH
clause coupled to a WHERE
part which adds restrictions, or predicates, to the MATCH patterns. But in that case everything that is returned by the query that you showed will be deleted.
You can follow the documentation about the MATCH clause that explain how it works: MATCH Documentation
Upvotes: 0
Reputation: 233
Everything that matches inside your query will be deleted. In your case you match all the vertices that have the label Useless
which means that it will search all the vertices that belong to that label and delete them.
If you want to become more specific you need to use the WHERE
clause for example:
SELECT * FROM cypher('graph_name', $$
MATCH (v:Useless) WHERE v.<property> = <value_you_want_to_filter>
DELETE v $$) as (v agtype);
This will filter the MATCH
clause with the value of the properties and delete only those vertices that satisfy the condition.
Upvotes: 0
Reputation: 48
As for the DELETE clause, it will delete all matching vertices with label 'useless' for example.
While DETACH DELETE will do the same along with detaching any edges/relationships that they have.
Also, it says in the docs that you cannot delete a node without deleting edges that start/end on the vertex in concern. So, you should use detach delete.
Upvotes: 0
Reputation: 675
If you do not filter out the vertices that you want to perform the delete operation on using the WHERE
clause or inside the MATCH
clause such as
1.
SELECT * FROM cypher('graph_name', $$
MATCH (u: Useless) WHERE u.xyz = 'xyz'
DELETE u RETURN u
$$) AS (result agtype);
SELECT * FROM cypher('graph_name', $$
MATCH (u: Useless {xyz = 'xyz'})
DELETE u RETURN u
$$) AS (result agtype);
then all the vertices that MATCH query returns will be deleted and there is no rule to just delete the first returned entity. Note that The same case is with the DETACH DELETE
that if you do not filter on the returned vertices, then all of the vertices and associated edges will be deleted.
Upvotes: 0