Search code examples
neo4jcyphernodesrelationships

Delete old nodes and relationships with Cypher in Neo4j 1.9


I am using Neo4j 1.9 M03 in HA mode, my challenge is to remove all nodes and relationships as well as indices that are older than a certain date.

For this I created a property for nodes and relationships. The property is a datestamp in the "YYMMDD" format.

I'm trying to use the following Cypher query to perform the operation mentioned above:

START n0=node(0), nx=node(*) 
MATCH n0-[r0?]-(), nx-[rx?]-() 
WHERE nx <> n0 AND HAS (nx.datestamp) AND nx.datestamp <= yyyymmdd 
OR HAS (rx.datestamp) AND rx.datestamp <= yyyymmdd
DELETE r0,rx,nx

This query is not performing the operation I desire. What can I be doing wrong here?


Solution

  • START rx=rel(*)
    WHERE HAS (rx.datestamp) AND rx.datestamp <= yyyymmdd
    DELETE rx;
    

    You have to make sure that the nodes you want to delete don't have any relationships left, either filter those out or you delete those additionally

    START nx=node(*)
    WHERE HAS (nx.datestamp) AND nx.datestamp <= yyyymmdd
    AND NOT((nx)--())
    DELETE nx;