# Cypher Query Validation

**URL:** <https://community.neo4j.com/t/cypher-query-validation/14064>\
**Category:** Cypher\
**Created:** [January 27, 2020, 9:24am UTC](https://community.neo4j.com/t/cypher-query-validation/14064 "2020-01-27T09:24:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [January 27, 2020, 9:24am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/1 "2020-01-27T09:24:08Z")

</div>

Hello team ,

Please help me in validating the cypher query for bulk deletion of data under specific label:

call apoc.periodic.iterate("MATCH (n:SITE) return n", "DETACH DELETE n", {batchSize:1000}) yield batches, total return batches, total

Regards  
Akshat

---

<div class="post-metadata">

**Author:** ![stefan.armbruster](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stefan.armbruster/32/74_2.png) [@stefan.armbruster](https://community.neo4j.com/u/stefan.armbruster)\
**Post date:** [January 27, 2020, 9:53am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/2 "2020-01-27T09:53:54Z")

</div>

This will work unless you have SITE nodes with a huge number (thinking millions or more) of relationships. In this case the `DETACH DELETE` will become too fat.

The way to handle this is to use two statements:

```auto
// delete relationships first
call apoc.periodic.iterate("MATCH (n:SITE)-[r]-() RETURN r", "DELETE r", {batchSize:1000});
// delete nodes second
call apoc.periodic.iterate("MATCH (n:SITE) RETURN n", "DETACH DELETE n", {batchSize:1000});

```

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [January 27, 2020, 10:25am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/3 "2020-01-27T10:25:11Z")

</div>

Hello Stefan,

Thanks for a quick reply.

I have 5 labels and their count respectively:  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/b/be2cda051a41f0ae41e43c858c3506a000f11586.png)  
These labels are related like site to container , container to shelf , shelf to card , card to port .  
Hence 4 relationship amongst 5 labels.

Best Regards  
Akshat

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [January 28, 2020, 12:22pm UTC](https://community.neo4j.com/t/cypher-query-validation/14064/4 "2020-01-28T12:22:43Z")

</div>

Hello Mate,

How to deal with these type of query which contains millions of data:

call apoc.periodic.iterate("MATCH (m:MO\_STATIC\_TABLE)\<-[r1:container2mostatic]-(c:CONTAINER) ,  
(sc:SITE\_CODE\_TABLE)\<-[R2]-(s:SITE)-[r4:site2container]-\>(c:CONTAINER)-[r5:container2shelf]-\>(sh:SHELF)-[r6:shelf2card]-\>(ca:CARD)-[r7:card2port]-\>(po:PORT) where c.EQUIP\_TYPE='RRH' and m.STD\_VENDOR='SAMSUNG'", "set po.PORT\_CUSTOM\_MANAGEDOBJECT='RIL\_SAMSUNG .'+m.STD\_TNS+'.'+c.EQUIP\_EMS\_INSTANCENAME+' NODE "'+po.PORT\_NATIVENAME+'"',  
po.PORT\_CUSTOM\_OPERATIONCONTEXT='.oc.'+sc.SITE\_CIRCLE\_CODE\_OC+'\_LSM-R'" , {batchSize:1000}) yield batches, total return "C 2 MO rel" , batches, total;

Best Regards  
Akshat

---

<div class="post-metadata">

**Author:** ![stefan.armbruster](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stefan.armbruster/32/74_2.png) [@stefan.armbruster](https://community.neo4j.com/u/stefan.armbruster)\
**Post date:** [January 28, 2020, 4:05pm UTC](https://community.neo4j.com/t/cypher-query-validation/14064/5 "2020-01-28T16:05:21Z")

</div>

The approach I've sketched above is exactly what you want to use when deleting a large number of nodes: first remove relationships then remove nodes. Both steps in reasonable batches.

What exactly is the issue now?

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [January 29, 2020, 4:46am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/6 "2020-01-29T04:46:01Z")

</div>

Hello Stefan,

Issue is server connection getting lost while running the above query which sets the 2 properties under specific label for lacs of records in it.  
So I need some faster way to do this.

Best Regards  
Akshat

---

<div class="post-metadata">

**Author:** ![stefan.armbruster](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stefan.armbruster/32/74_2.png) [@stefan.armbruster](https://community.neo4j.com/u/stefan.armbruster)\
**Post date:** [January 29, 2020, 8:49am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/7 "2020-01-29T08:49:08Z")

</div>

Which version are you using? Both neo4j and apoc version.

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [January 29, 2020, 9:24am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/8 "2020-01-29T09:24:09Z")

</div>

Hello Stefan ,

Neo4j Version = 3.5.3  
APOC Version = apoc-3.5.0.4-all.jar

Best Regards  
Akshat

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [January 29, 2020, 9:29am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/9 "2020-01-29T09:29:30Z")

</div>

This is the error we got -

[root@jmngdprv009693 bin]# ./neo4j stop  
Stopping Neo4j......................................................................................................................... failed to stop  
Neo4j (pid 76877) took more than 120 seconds to stop.  
Please see /datasan01/neo4japp/neo4j-enterprise-3.5.3/logs/neo4j.log for details.  
[root@jmngdprv009693 bin]# ./neo4j status  
Neo4j is running at pid 76877  
[root@jmngdprv009693 bin]# kill -9 76877  
[root@jmngdprv009693 bin]# ./neo4j status  
Neo4j is running at pid 76877  
[root@jmngdprv009693 bin]# ./neo4j status  
Neo4j is not running  
[root@jmngdprv009693 bin]# ./neo4j start  
Active database: graph.db  
Directories in use:  
home: /datasan01/neo4japp/neo4j-enterprise-3.5.3  
config: /datasan01/neo4japp/neo4j-enterprise-3.5.3/conf  
logs: /datasan01/neo4japp/neo4j-enterprise-3.5.3/logs  
plugins: /datasan01/neo4japp/neo4j-enterprise-3.5.3/plugins  
import: /datasan01/neo4japp/neo4j-enterprise-3.5.3/import  
data: /datasan01/neo4japp/neo4j-enterprise-3.5.3/data  
certificates: /datasan01/neo4japp/neo4j-enterprise-3.5.3/certificates  
run: /datasan01/neo4japp/neo4j-enterprise-3.5.3/run  
Starting Neo4j.  
Started neo4j (pid 903). It is available at [http://0.0.0.0:7474/](http://0.0.0.0:7474/)  
There may be a short delay until the server is ready.  
See /datasan01/neo4japp/neo4j-enterprise-3.5.3/logs/neo4j.log for current status.  
[root@jmngdprv009693 bin]# ./neo4j status  
Neo4j is running at pid 903  
[root@jmngdprv009693 bin]#

---

<div class="post-metadata">

**Author:** ![stefan.armbruster](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stefan.armbruster/32/74_2.png) [@stefan.armbruster](https://community.neo4j.com/u/stefan.armbruster)\
**Post date:** [January 29, 2020, 11:34am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/10 "2020-01-29T11:34:46Z")

</div>

I don't remember in which apoc version we've fixed this. In the old days you needed to prefix the first statement with `CYPHER runtime=slotted` - otherwise it could have happened that the query result gets fully materialized in heap before the second statement is run.

```auto
call apoc.periodic.iterate("CYPHER runtime=slotted MATCH ....", "...", { ...})

```

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [February 3, 2020, 12:19pm UTC](https://community.neo4j.com/t/cypher-query-validation/14064/11 "2020-02-03T12:19:07Z")

</div>

Hello Stefan,

Getting below error when i added the runtime=slotted .

call apoc.periodic.iterate("CYPHER runtime=slotted MATCH (m:MO\_STATIC\_TABLE)\<-[r1:container2mostatic]-(c:CONTAINER) ,(sc:SITE\_CODE\_TABLE)\<-[R2]-(s:SITE)-[r4:site2container]-\>(c:CONTAINER)-[r5:container2shelf]-\>(sh:SHELF)-[r6:shelf2card]-\>(ca:CARD)-[r7:card2port]-\>(po:PORT) where c.EQUIP\_TYPE='RRH' and m.STD\_VENDOR='SAMSUNG'", "set po.PORT\_CUSTOM\_MANAGEDOBJECT='RIL\_SAMSUNG .'+m.STD\_TNS+'.'+c.EQUIP\_EMS\_INSTANCENAME+' NODE "'+po.PORT\_NATIVENAME+'"',po.PORT\_CUSTOM\_OPERATIONCONTEXT='.oc.'+sc.SITE\_CIRCLE\_CODE\_OC+'\_LSM-R'" , {batchSize:1000}) yield batches, total return "C 2 MO rel" , batches, total;

Error:  
Neo.ClientError.Statement.SyntaxError: Invalid input ''': expected whitespace, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '\*', '/', '%', '+', '-', '=', '~', "\<\>", "!=", '\<', '\>', "\<=", "\>=", AND, XOR, OR, ',' or ')' (line 1, column 426 (offset: 425))  
"call apoc.periodic.iterate("CYPHER runtime=slotted MATCH (m:MO\_STATIC\_TABLE)\<-[r1:container2mostatic]-(c:CONTAINER) ,(sc:SITE\_CODE\_TABLE)\<-[R2]-(s:SITE)-[r4:site2container]-\>(c:CONTAINER)-[r5:container2shelf]-\>(sh:SHELF)-[r6:shelf2card]-\>(ca:CARD)-[r7:card2port]-\>(po:PORT) where c.EQUIP\_TYPE='RRH' and m.STD\_VENDOR='SAMSUNG'", "set po.PORT\_CUSTOM\_MANAGEDOBJECT='RIL\_SAMSUNG .'+m.STD\_TNS+'.'+c.EQUIP\_EMS\_INSTANCENAME+' NODE "'+po.PORT\_NATIVENAME+'"',po.PORT\_CUSTOM\_OPERATIONCONTEXT='.oc.'+sc.SITE\_CIRCLE\_CODE\_OC+'\_LSM-R'" , {batchSize:1000}) yield batches, total return "C 2 MO rel" , batches, total;"  
^

Best Regards  
Akshat

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 3, 2020, 6:09pm UTC](https://community.neo4j.com/t/cypher-query-validation/14064/12 "2020-02-03T18:09:50Z")

</div>

You're running into a nested quotes issue. In the driving query (first query) of the iterate() call you're using double quotes around the query, with single quotes as needed around strings in that query. That one's fine.

But your updating query (the second one) has double-quotes in it, which terminates the string, and you're not handling the rest properly. If you do need to use double-quotes in your double-quote delimited string, you need to escape them so they don't end your string early and unexpectedly.

---

<div class="post-metadata">

**Author:** ![akshat\_mittal](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/akshat_mittal/32/4359_2.png) [@akshat\_mittal](https://community.neo4j.com/u/akshat_mittal)\
**Post date:** [February 5, 2020, 7:44am UTC](https://community.neo4j.com/t/cypher-query-validation/14064/13 "2020-02-05T07:44:19Z")

</div>

Hello Andrew,

Is this update correct from your side? I believe 2 queries ( posted by me ) is mixed up here.

Best Regards  
Akshat
