Search code examples
neo4jneo4j-apoc

Mass delete empty properties in a Neo4j database


I have a Neo4j database with 100M nodes. A lot of those nodes contain empty properties and I would like to remove these properties.

I have tried the following query:

:auto MATCH (n)
WITH n
call { with n
UNWIND keys(n) as k
WITH n, k 
WHERE n[k] = ''
WITH n, collect(k) as propertyKeys
CALL apoc.create.removeProperties(n, propertyKeys)
YIELD node
RETURN node
} in transactions of 50000 rows;

I get the following error message:

Query cannot conclude with CALL (must be a RETURN clause, an update clause, a unit subquery call, or a procedure call with no YIELD) (line 3, column 1 (offset: 19)) "call { with n" ^

Can someone tell me what I'm doing wrong and how to fix that?

Thanks for your help !


Solution

  • I propose a counter solution to your query. Below is using apoc iterate function which will extract the data with empty property and execute the removal of the property by batch (50k) in parallel.

    CALL apoc.periodic.iterate(
      "MATCH (n) UNWIND keys(n) as k WITH n, k WHERE n[k] = '' RETURN n, k",
      "WITH n, collect(k) as propertyKeys
       CALL apoc.create.removeProperties(n, propertyKeys) YIELD node
       RETURN node",
      {batchSize:50000, parallel:true})
    

    To explain the error you are getting, a subquery call cannot use another call that requires a yield function. It is mentioned in the error message at the end

    qoute: Query cannot conclude with CALL with another procedure call with YIELD