Reputation: 23
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?
Upvotes: 2
Views: 1281
Reputation: 522
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;
Upvotes: 0
Reputation: 29172
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
Upvotes: 3
Reputation: 11216
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.
Upvotes: 1