cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

BASIC: Delete a single relationship between two nodes

I have two specific nodes, that I can identify with unique ids. Super simple - how do I remove a single likes relationship between the two? This doesn't work for some reason, nothing is returned and there is no difference to the data in Neo4J. Note the relationship doesn't have any properties.

MATCH (u:User)-[r:LIKES]-(p:Post)
WHERE u.id="95e54ae5-e512-4030-9640-6b157aaf5400" AND p.id="919f3216-b13b-48ae-a4df-ef779325ff5e"
DELETE r

I've tried this as well, also doesn't work

MATCH (u:User {id: "95e54ae5-e512-4030-9640-6b157aaf5400"})-[r:LIKES]-(p:Post {id: "919f3216-b13b-48ae-a4df-ef779325ff5e"})
DELETE r

What am I missing?

1 ACCEPTED SOLUTION

In looking at the screen shot where the values are shown on the bottom of the screen, the ID for the User node has a '55' in it, but the value being used in the match query has a '54' in it. It appears the values being used in the query to do the deletion don't match the values in the database.

I tried this example and it worked fine. First creating the data values to delete:

create (u:User {id:'95e54ae5-e512-4030-9640-6b157aaf5400'})
create (p:Post {id:'919f3216-b13b-48ae-a4df-ef779325ff5e'})
merge (u)-[:LIKES]->(p)

That created the nodes and the relationship.
Added 2 labels, created 2 nodes, set 2 properties, created 1 relationship, completed after 2 ms.

Then they deleted fine using the initial query that was posted:

MATCH (u:User)-[r:LIKES]-(p:Post)
WHERE u.id="95e54ae5-e512-4030-9640-6b157aaf5400" AND p.id="919f3216-b13b-48ae-a4df-ef779325ff5e"
DELETE r

With the results:
Deleted 1 relationship, completed after 2 ms.

Double check the values being passed in as the IDs. On the screen shots were both the query values and database values are shown, the values in the query do not match the values in the database.

View solution in original post

14 REPLIES 14

Hello @ali.ahmed and welcome to the Neo4j community

MATCH (u:User {id: "95e54ae5-e512-4030-9640-6b157aaf5400"})-[r:LIKES]-(p:Post {id: "919f3216-b13b-48ae-a4df-ef779325ff5e"})
DETACH DELETE r

Regards,
Cobra

Hi and thanks! That code doesn't work either. Any idea why?

That's weird, try to specify the direction:

MATCH (u:User {id: "95e54ae5-e512-4030-9640-6b157aaf5400"})-[r:LIKES]->(p:Post {id: "919f3216-b13b-48ae-a4df-ef779325ff5e"})
DETACH DELETE r

Can you check that ids are right?

The ids are right and truly unique, tried that and didn't work... what's odd is that if I create two new separate nodes that only contain this singular link, that code works

Here's a screenshot of my database as of now:

The two nodes have these properties (if it makes a difference):

Not sure which relationship or node is preventing me from having this query work as expected. I changed the script to include the right ids btw! Ran this and the output is below:

In looking at the screen shot where the values are shown on the bottom of the screen, the ID for the User node has a '55' in it, but the value being used in the match query has a '54' in it. It appears the values being used in the query to do the deletion don't match the values in the database.

I tried this example and it worked fine. First creating the data values to delete:

create (u:User {id:'95e54ae5-e512-4030-9640-6b157aaf5400'})
create (p:Post {id:'919f3216-b13b-48ae-a4df-ef779325ff5e'})
merge (u)-[:LIKES]->(p)

That created the nodes and the relationship.
Added 2 labels, created 2 nodes, set 2 properties, created 1 relationship, completed after 2 ms.

Then they deleted fine using the initial query that was posted:

MATCH (u:User)-[r:LIKES]-(p:Post)
WHERE u.id="95e54ae5-e512-4030-9640-6b157aaf5400" AND p.id="919f3216-b13b-48ae-a4df-ef779325ff5e"
DELETE r

With the results:
Deleted 1 relationship, completed after 2 ms.

Double check the values being passed in as the IDs. On the screen shots were both the query values and database values are shown, the values in the query do not match the values in the database.

Thanks! Yeah odd it started working out of the blue even though I didn't change anything. Thanks for all the help!

Indeed, I check the doc and your first query should work. Which version of Neo4j are you using? Maybe try to delete with APOC?

Using the following:
Neo4j Browser version: 4.1.2

Neo4j Server version: 4.1.3 (enterprise)

Do you know which apoc function deletes a single relationship? I can only find apoc functions that delete nodes.

Something like this:

CALL apoc.periodic.iterate('MATCH (u:User {id: "95e54ae5-e512-4030-9640-6b157aaf5400"})-[r:LIKES]-(p:Post {id: "919f3216-b13b-48ae-a4df-ef779325ff5e"}) RETURN r', 'DELETE r', {batchSize:1000, params:{}})

odd even that didn't work could it be the 'HAS_TAG' relationship going in the other direction somehow interfering?

I don't really know what to doother than slowly add relationships to the node / see which relationship is causing the thing not to delete, but this seems relatively basic and something that people should have run into so I'm very very confused.

Going to put in updates here:
(1) this works, but obvious it deletes all the likes relationships not the specific one I want.

MATCH (u)-[r:LIKES]->(p)
DELETE r

(2) If there are no HAS_TAG relationships coming out of the User node, then I run the above script and it works. See above screenshot, this is as if the only relationship coming out of the User node labeled 'Ali' is LIKES.
(3) When there are only two relationships between that user and the post, HAS_TAG and LIKES as such, I can run that above script and things work...

(u:User)-[:LIKES]->(p:Post)
AND
(u:User)<-[:HAS_TAG]-(p:Post)

(4) BIG ONE - I can't actually remove any LIKES, or HAS_TAG relationships between the node and the post for the node labelled 'ali' in the below photo. The query for deleting any single relationship for a user stops working. Note it was working when there was just the one HAS_TAG and Likes relationship between the post labelled "yooo" and the user node labelled "ali".

Question is, why? @Cobra any idea? I'm sure this has something to do with cycles but I thought the ids would make this a non-issue.

It should work normally since you specified both ids, I have no idea about what is going on

You can't reference id as an attribute of the node. The correct syntax is:

MATCH (u:User)-[r:LIKES]-(p:Post)
WHERE ID(u)="95e54ae5-e512-4030-9640-6b157aaf5400" AND ID(p)="919f3216-b13b-48ae-a4df-ef779325ff5e"
DELETE r

None of the above worked for me and here's my syntax:

MATCH (c:Claim)-[r:CLAIM_HAS_CLAIMCATEGORY]-(cc:ClaimCategory)
WHERE id(c)="b3542c32-b99d-4fff-b15e-05d5403030c5" AND id(cc)="4fa2ad0f-2630-4226-a000-c82852127a66"
DELETE r

I did not see deleteing a specific relationship documented in neo4j.

Any help on this?