0

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.

Telmo Trooper
  • 4,993
  • 1
  • 30
  • 35
  • Something to take into account. When deleting big dbs via this method, even though the DB will be empty, the db file will still use the same amount of space in the file system that it was using before emptied. – HerrIvan Sep 27 '22 at 12:40

1 Answers1

1

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/

TheTeacher
  • 500
  • 2
  • 6
  • 1
    Supplementing, this is a consequence of Neo4j being an ACID db. Transactions must be atomic, but the transactional state can exceed heap, so as recommended it's best to batch large transactions to ease heap pressure. – InverseFalcon Dec 30 '19 at 15:42