How to return number of deleted nodes when using apoc.periodic.iterate?

Hi all,

I realize when deleting nodes, there is no information returned like number of deleted nodes or even number of delete relationships.
Example query

CALL apoc.periodic.iterate('MATCH (u:User) 
WHERE u.name IN $data
OPTIONAL MATCH (u)-[:OWNS]->(c:Car) 
RETURN u, c', 
'DETACH DELETE u, c', {batchSize:1000, params:{data:$data}})

In the above example, I want to return the number of user nodes deleted and number of car nodes deleted if it is possible with the additional info I get from using apoc.periodic.iterate
Thanks in advance.

You can YIELD the total value from the procedure call to get the total number of rows:

CALL apoc.periodic.iterate('MATCH (u:User) 
WHERE u.name IN $data
OPTIONAL MATCH (u)-[:OWNS]->(c:Car) 
RETURN u, c', 
'DETACH DELETE u, c', {batchSize:1000, params:{data:$data}})
YIELD total
RETURN total

You can yield any of the values below:

(batches :: INTEGER?, total :: INTEGER?, timeTaken :: INTEGER?, committedOperations :: INTEGER?, failedOperations :: INTEGER?, failedBatches :: INTEGER?, retries :: INTEGER?, errorMessages :: MAP?, batch :: MAP?, operations :: MAP?, wasTerminated :: BOOLEAN?, failedParams :: MAP?)

Hello @adam.cowley

Thanks for the reply

Based on your reply,
It only returns the total rows but doesn't return the total number of nodes deleted. In the query I have also match the user to car this is a one-to-many relationship thus I can be deleting a lot more nodes then rows. I would also like to know if possible the number of nodes deleted for each (i.e. number of nodes deleted for user and number of nodes deleted for cars).

Hello @tarendran.vivekanand :slight_smile:

One solution is to count nodes before and count after and compute the difference :slight_smile:

Regards,
Cobra

1 Like

Hello @Cobra :slight_smile:
Thank you. More work though :frowning:

1 Like