First, you must install GDS plugin on your database.
Then, you create a graph projection on the whole database:
CALL gds.graph.project(
"graph",
"*",
"*"
)
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels
Then you run the WCC algorithm in write mode (each subgraph is a component and will have its own componentId
):
CALL gds.wcc.write('graph', { writeProperty: 'componentId' })
YIELD nodePropertiesWritten, componentCount;
Then you delete the graph projection which is stored in-memory:
CALL gds.graph.drop('graph', false) YIELD graphName;
Finally, you can delete the subgraph based on the componentId
(the query deletes the subgraph which has 0 as componentId
) property stored on nodes (the query works on at least Neo4j 4.4 version):
MATCH (n)
WHERE n.componentId = 0
CALL {
WITH n
DETACH DELETE n
} IN TRANSACTIONS OF 10000 ROWS;
Or this query if you are under Neo4j 4.4 version:
CALl apoc.periodic.iterate(
"MATCH (n) WHERE n.componentId = 0 RETURN n",
"DETACH DELETE n", {batchSize: 10000}
)
YIELD batches, total
RETURN batches, total
Regards,
Cobra