How to delete millions of relationships off a single node with Cypher - neo4j

I have created a large graph in Neo4j and have an empty node that is connected via 11 million relationships in graph that I need to remove. I know that if I just delete the node I will leave behind all the hanging relationships but I have been unsuccessful in my attempts to remove them. I have tried the following CYPHER commands but they hang and fail to complete:
MATCH (n:Label {uid: ''}) DETACH DELETE n;
and
MATCH (n:Label {uid: ''})-[r]-() DELETE r;
I'm working under the assumption that there are not enough resources to load the 11 million relationship subgraph in memory in order to detach and delete the node. Is there any way to loop over the relationships in order to lower the required system resources?

1) You can use apoc.periodic.commit function from the apoc library:
call apoc.periodic.commit(
'MATCH (n:Label {uid: {uid}})-[r]->()
WITH r LIMIT {limit}
DELETE r
RETURN COUNT(r)', {
limit: 1000,
uid: ...
})
2) You can delete the node, and then create it again with apoc.create.node function:
MATCH (n:Label {uid: 2})
WITH n, {labels: labels(n), properties: properties(n)} AS data
DETACH DELETE n
WITH data
CALL apoc.create.node(data.labels, data.properties) yield node AS newNode
RETURN newNode

You could delete the relationships in batches and then delete the node
MATCH (n:Label {uid: ''})-[r]-()
WITH r
LIMIT 1000
DELETE r;
If you run that successively you will delete the relationships in small batches. Play with the limit amount to see what your running system will tolerate resource wise.

If you are on Neo4j 4.0 or above, then this is the best way:
MATCH (n:Label {uid: ''})-[r]-()
CALL {
WITH r
DELETE r
} IN TRANSACTIONS OF 1000 ROWS;

Related

Neo4j Delete All Nodes and Relationships

I want to delete all nodes and relationships.
I run MATCH ()-[r]-() DELETE r to delete all the relationships.
Then, I run MATCH (n) DELETE n to delete all the nodes. It does delete all the nodes, but the problem is that it also gives me this error:
Neo.DatabaseError.Transaction.TransactionCommitFailed
Unable to complete transaction.
How do I delete all nodes and relationships with getting this error?
To delete all nodes and all relationships, I do DETACH DELETE
Reference:
https://neo4j.com/docs/cypher-manual/current/clauses/delete/#delete-delete-all-nodes-and-relationships
MATCH (n)
DETACH DELETE n;
If nothing works, then rename (or remove) your neo4j data folder and restart your server.
<HOME_NEO4j>/data/data/transactions/neo4j
It looks like your database is hitting an OOM Exception.
As stated in Large Delete Transaction Best Practices in Neo4j
If you need to delete some large number of objects from the graph, one needs to be mindful of the not building up such a large single transaction such that a Java OUT OF HEAP Error will be encountered.
Delete all constraints and indexes
CALL apoc.schema.assert({},{},true);
Batch delete with CALL {} IN TRANSACTIONS syntax or apoc.periodic.iterate:
MATCH (n:Foo) where n.foo='bar'
CALL { WITH n
DETACH DELETE n
} IN TRANSACTIONS OF 10000 ROWS;
OR
CALL apoc.periodic.iterate(
"MATCH (n) RETURN n",
"DETACH DELETE n",
{batchSize:10000, parallel:false})

How to Delete a Node in Neo4j using Cypher Graph Query Language?

I want to Delete this Pessoa node and its relations to others,
but I don't want to delete the other nodes.
This node has a Guid ID property with value c40f314f-0ecf-42e1-b44d-85b6d72f134a
I tried
MATCH (n {ID: 'c40f314f-0ecf-42e1-b44d-85b6d72f134a'}) DELETE n;
But this ERROR appears:
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<35>, because it still has relationships. To delete this node, you must first delete its relationships.
Using
MATCH (n {ID: 'c40f314f-0ecf-42e1-b44d-85b6d72f134a'}) DETACH DELETE n;
Deleted 1 node, deleted 2 relationships, completed after 2 ms.
Note that relationships of the node were removed as well.

How to check if nodes are connect at all in Neo4j

I have a graph in Neo4j (first time using it) of about 10 different nodes that are connected in various ways. Not all nodes are connected to each other, as some have up to 6 or 7 neighbors, while some have only 1. What query would I write/use to check if a path exists from NodeA to NodeB? It doesn't have to be the shortest path, just if a path exists.
Along with this, is there a way to count who has the most or least neighbors? Thanks everyone for help in advance.
Return Foo nodes a and b if there is at least one path between them. (This variable-length path query with unbounded length could take a very long time or run out of memory if there are a lot of paths or very long paths).
MATCH (a:Foo {id: 'a'}), (b:Foo {id: 'b'})
WHERE (a)-[*]-(b)
RETURN a, b;
Return all paths between a and b. (This query could require even more time and memory than the previous query, since it will attempt to return all matching paths).
MATCH path=(a:Foo {id: 'a'})-[*]-(b:Foo {id: 'b'})
RETURN path;
Return the 10 nodes with the most neighbors, in descending order:
MATCH (n)--()
WITH n, COUNT(*) AS c
RETURN n
ORDER BY c DESC
LIMIT 10;

How to delete lots of nodes

I want to delete all nodes of a given type and their relations. In total there are 1.4 million nodes of this type.
Using MATCH (n:Type) DETACH DELETE n Neo4j hangs itself up after a few minutes and has to be restarted.
Is there a better way to delete a large number of nodes? Can I delete them in chunks somehow (LIMIT is not supported with DELETE)?
Try this
Match (n:Type) with n
Match (n)-[r]-()
Delete n, r
If you want to delete them in chunks the query would look like
Match (n:Type) with n limit 1000
Match (n)-[r]-()
Delete n, r

Neo4j: Get all nodes in a graph, even those that are unconnected by relationships

Using Cypher how can I get all nodes in a graph? I am running some testing against the graph and I have some nodes without relationships so am having trouble crafting a query.
The reason I want to get them all is that I want to delete all the nodes in the graph at the start of every test.
So, this gives you all nodes:
MATCH (n)
RETURN n;
If you want to delete everything from a graph, you can do something like this:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n, r;
Updated for 2.0+
Edit:
Now in 2.3 they have DETACH DELETE, so you can do something like:
MATCH (n)
DETACH DELETE n;
Would this work for you?
START a=node:index_name('*:*')
Assuming you have an index with these orphaned nodes in them.
This just works fine in 2.0:
MATCH n RETURN n
If you need to delete some large number of objects from the graph, one needs to be mindful of the not building up such a large single transaction such that a Java OUT OF HEAP Error will be encountered.
If your nodes have more than 100 relationships per node ((100+1)*10k=>1010k deletes) reduce the batch size or see the recommendations at the bottom.
With 4.4 and newer versions you can utilize the CALL {} IN TRANSACTIONS syntax.
MATCH (n:Foo) where n.foo='bar'
CALL { WITH n
DETACH DELETE n
} IN TRANSACTIONS OF 10000 ROWS;
With 3.x forward and using APOC
call apoc.periodic.iterate("MATCH (n:Foo) where n.foo='bar' return id(n) as id", "MATCH (n) WHERE id(n) = id DETACH DELETE n", {batchSize:10000})
yield batches, total return batches, total
For best practices around deleting huge data in neo4j, follow these guidelines.

Resources