Running "MATCH (n) DETACH DELETE n" via "neo4j-driver" doesn't work

677 Views Asked by At

I'm testing the neo4j-driver package to run Cypher queries via JavaScript/TypeScript.

I can run most queries just fine, but every time I try running the command MATCH (n) DETACH DELETE n my program just keeps hanging and nothing happens.

My code:

// main.ts

import neo4j from "neo4j-driver"

const main = async () => {
  const driver = neo4j.driver("bolt://localhost:7687",
    neo4j.auth.basic("neo4j", "telmo"))

  const session = driver.session()

  console.log("This command works fine")
  await session.run(`CREATE (n:Person {name: "Bob"}) RETURN n.name`)

  console.log("This one does not")
  await session.run("MATCH (n) DETACH DELETE n")

  console.log("The code never even gets here")

  session.close()
  driver.close()
}

main()

Does anyone know why the program hangs on MATCH (n) DETACH DELETE n and what can I do to fix it? Do notice that my database is only for testing and has a very small quantity of data.

1

There are 1 best solutions below

1
On

when you do match(n) detach delete n ..it loads all the relations and nodes in graph and your heap size may be not enough to load all the data at once. try the following query instead which does the job batchwise , CALL apoc.periodic.iterate( "MATCH (n) RETURN n", "DETACH DELETE n", {batchSize:10000, parallel:false})

read more about apoc.iterate here http://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/cypher-execution/commit-batching/