Delete operation in SDN using cypher

61 views
Skip to first unread message

Abdul Azeez Shaik

unread,
Sep 26, 2012, 6:33:39 AM9/26/12
to ne...@googlegroups.com
Dear all,

Can i delete a node along with its relationships and children using the following CYPHER query, 
"START g = node:galleryid(galleryid="0511a491-62ea-41f6-9a6f-b9060e3c525a") MATCH g-[r]-p DELETE g, r, p"

Its not working when i tried this in webconsole of neo4j.
I am trying to find where i can delete a node with all its children at one go.
Or do i need to delete each children and its relationships, and then finally parent node.

Kindly let me know your suggestions.

Thanks,
Abdul

Nigel Small

unread,
Sep 26, 2012, 7:19:47 AM9/26/12
to ne...@googlegroups.com
Hi Abdul

I have recently added cascading deletion to py2neo. I use the following Cypher query...

START a=node(1)
MATCH (a)-[rels*0..]-(z)
FOREACH(rel IN rels: DELETE rel)
DELETE a, z

...where the first line determines the start node by ID. I have also considered looking at limits on relationship type and direction as well as a maximum cascade limit so you may want to consider that too.

Cheers
Nige


--
 
 

Lasse Westh-Nielsen

unread,
Sep 26, 2012, 7:33:26 AM9/26/12
to ne...@googlegroups.com
Abdul,

Firstly, did you mean to specify relationships in both directions? Or did you mean (notice the arrow to 'p'): "START g = node:galleryid(galleryid="0511a491-62ea-41f6-9a6f-b9060e3c525a") MATCH g-[r]->p DELETE g, r, p"?

Anyway that should work if your children have no other relationships. If you want to, you can subtrees like this: "START g = node:galleryid(galleryid="0511a491-62ea-41f6-9a6f-b9060e3c525a") MATCH p=g-[*]->() DELETE p"

A detail you need to note is the index name, when you use SDN: you can either use the automatic/ convention based one, or you can give it a custom name.

Worth writing a test I think? :)

Lasse




--
 
 

Abdul Azeez Shaik

unread,
Sep 26, 2012, 9:43:33 AM9/26/12
to ne...@googlegroups.com
Hi Nigel,

Thanks for the reply. I tried executing in latest 1.8RC1 webconsole, it gives me following error,
TransactionFailureException: Unable to commit transaction

Also, Do we need to use FOREACH in this case? Can't we directly use DELETE a,rels,z? 
In my case a and z are also collections, not single node.

Thanks,
 Abdul

--
 
 

Abdul Azeez Shaik

unread,
Sep 26, 2012, 9:50:05 AM9/26/12
to ne...@googlegroups.com
Hi Lasse,

Thanks for the reply. As you rightly pointed out, i want to specify the relationship in outgoing.
I don't want to delete the nodes which has incoming relationships. That could be reason for throwing exception.

My requirement is following:
Delete node + its all relationships + only children with outgoing relationships.

Will try to see if i can come up with an relevant query for this.

Thanks,
Abdul

--
 
 

Abdul Azeez Shaik

unread,
Sep 26, 2012, 1:26:40 PM9/26/12
to ne...@googlegroups.com
Hi Lasse,

Can you construct a query for me where only children with outgoing relationships must be deleted?
I am struggling to write a query for thsi

requirement:
node
its relationships,
its children with outgoing relationship


On Wed, Sep 26, 2012 at 5:03 PM, Lasse Westh-Nielsen <lasse.wes...@neopersistence.com> wrote:
--
 
 

Lasse Westh-Nielsen

unread,
Sep 27, 2012, 7:06:38 AM9/27/12
to ne...@googlegroups.com
On Wed, Sep 26, 2012 at 6:26 PM, Abdul Azeez Shaik <azeezte...@gmail.com> wrote:
Hi Lasse,

Can you construct a query for me where only children with outgoing relationships must be deleted?
I am struggling to write a query for thsi

requirement:
node
its relationships,
its children with outgoing relationship

Heh, you are not the only one struggling with this one :)

Can anyone help? I'm drawing blanks...

Lasse

 

Michael Hunger

unread,
Sep 27, 2012, 10:18:14 AM9/27/12
to ne...@googlegroups.com
Something like:

START a=node(1)
MATCH (a)-[rels*0..]-(z)
FOREACH(rel IN rels: DELETE rel)
DELETE a
       WITH z
       where z-->()
DELETE z

Michael

--
 
 

Abdul Azeez Shaik

unread,
Sep 27, 2012, 11:48:20 PM9/27/12
to ne...@googlegroups.com
Hi Michael,

Thanks for the reply.
I made few changes to the below one and using following query. It does following
  1. Deletes all outgoing relationships and its children
  2. Deletes all incoming relationships and itself
START n=node:galleryid(galleryid="41d1b2bc-7f6e-42ac-8265-d208e73e0517") MATCH n-[rels*1..]->p FOREACH(rel IN rels: DELETE rel) DELETE p WITH n MATCH n<-[r]-() DELETE n, r;

It works fine. 
Please let me know if there are other best ways of achieving the same.

Thanks,
Abdul

--
 
 

Reply all
Reply to author
Forward
0 new messages