Search code examples
neo4jcypherlimit

Cypher query limit results and delete


I am trying to remove 20000 nodes which have datestamp property = 20130808 but when I replace "DELETE nx" with "RETURN COUNT(nx)" the result is 7880 and not 20000, at this moment I have 1000000 nodes in Neo4j 1.9.2. How can I make that correctly?

Cypher query:

START nx=node(*)
WITH nx
LIMIT 20000
WHERE HAS (nx.datestamp) AND nx.datestamp = 20130808 AND ID(nx) <> 0
DELETE nx

Solution

  • Thats because you first choose 20k arbitrary nodes and then apply the WHERE filter. You have to do it the other way round:

    START nx=node(*)
    WHERE HAS (nx.datestamp) AND nx.datestamp = 20130808 AND ID(nx) <> 0
    WITH nx
    LIMIT 20000
    DELETE nx
    

    Be aware that this kind of global operation with property access is expensive. A better approach would be to enable autoindexing for datestamp and then do:

    START n=node:node_auto_index(datestamp=20130808)
    WITH n
    LIMIT 20000
    DELETE n