Delete using Periodic Iterate not working

614 Views Asked by At

I'm using this query to delete a lot of nodes, and it works fine in 4.0.0 but using 4.1.1 it doesn't seem to work

CALL apoc.periodic.iterate('
MATCH (s:Thing{id:$thingId})
  CALL apoc.path.subgraphNodes(s, {}) YIELD node
  RETURN node',
'DETACH DELETE node',
{batchSize:2000, iterateList:true, parallel:false, params: { thingId: $idOfThingToDelete}}) YIELD total, batches, errorMessages
RETURN total

It errors with the following

Failed to invoke procedure apoc.periodic.iterate: Caused by: org.neo4j.graphdb.NotFoundException: Node 78063 not found

And then further down in the log, we get this

Failed to invoke procedure apoc.periodic.iterate: Caused by: java.lang.IllegalStateException: NOT PART OF CHAIN! RelationshipTraversalCursor[id=64250, open state with: denseNode=false, next=64250, mode=regular, underlying record=Relationship[64250,used=true,source=86327,target=86330,type=7,sPrev=64238,sNext=-1,tCount=1,tNext=-1,prop=478773,secondaryUnitId=-1,!sFirst, tFirst]]

I can see the node with id 78063 and it looks fine, so I don't understand why it's saying it can't find it.

Any help appreciated, thanks!

Update

This is the second query I tried, which uses distinct, but it still gives me a Node 12345 not found error

CALL apoc.periodic.iterate(
    'MATCH (t:Thing{id:$thingId})
    CALL apoc.path.subgraphNodes(s, {}) YIELD node
    RETURN DISTINCT node',
    'DETACH DELETE n',
    {batchSize:2000, iterateList:true, parallel:false, params: { thingId: $idOfThingToDelete}}) YIELD total, batches, errorMessages
RETURN total
1

There are 1 best solutions below

5
On

The apoc.path.subgraphNodes procedure can return the same node multiple times, so it should use RETURN DISTINCT node to ensure that each node is only returned once.

For instance:

CALL apoc.periodic.iterate(
  'MATCH (s:Thing{id:$thingId})
   CALL apoc.path.subgraphNodes(s, {}) YIELD node
   RETURN DISTINCT node',
  'DETACH DELETE node',
  {batchSize:2000, iterateList:true, parallel:false,
   params: {thingId: $idOfThingToDelete}}) YIELD total, batches, errorMessages
RETURN total