# How to remove connected components less than x nodes?

**URL:** https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166
**Category:** Cypher
**Created:** [August 27, 2020, 3:53pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166 "2020-08-27T15:53:33Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 27, 2020, 3:53pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/1 "2020-08-27T15:53:33Z")

</div>

I would like to remove small networks (connected components) that have less than x nodes.  
So, if the network component has x nodes or less, the nodes and the edges that belong to this component will be deleted.

Is that doable ?

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 28, 2020, 9:13am UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/2 "2020-08-28T09:13:22Z")

</div>

Hello @1113 🙂

Yeah, it looks possible. Can you show us a little example with an image with what you want to keep and what you want to delete?

Regards,  
Cobra

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 28, 2020, 12:34pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/3 "2020-08-28T12:34:19Z")

</div>

Hi,

Thank you for your reply 🙂  
What I would like to remove are the nodes/edges in the red area.

 ![6-22-2020 10-15-02 AM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/9/993f38dc70c0881f738b1dab59f79937d4f2400a.png)

Regards,

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 28, 2020, 12:50pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/4 "2020-08-28T12:50:00Z")

</div>

Can you execute `CALL db.schema.visualization()` on your database and show us the result please?

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 28, 2020, 2:06pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/5 "2020-08-28T14:06:17Z")

</div>

I reduce the amount of data so it will probably be more clear.  
Below an example of what I would like to do :  
In the green circle the Nodes/Edges I would like to keep.  
I would like to remove the rest because they are smaller than 6 nodes.

 ![8-28-2020 15-55-38 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/3/3bedf100b5df45317adfbaa49c0ebbe7c7a9e91c.png)

Attached the result of CALL db.schema.visualization()  
[Schema.txt](https://community.neo4j.com/uploads/short-url/tDqEozYTahEJvI2yRmWTrquloNq.txt) (1.1 KB)

And The csv files that I used as data source  
[Edges.txt](https://community.neo4j.com/uploads/short-url/iqUsHRIhsb0kPmII8bcIx2npDBJ.txt) (1.3 KB) [Nodes.txt](https://community.neo4j.com/uploads/short-url/y5bqCZQtrr0WcuhxkvQNBcfVnZ.txt) (869 Bytes)

Thanks in advance !

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 28, 2020, 3:20pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/6 "2020-08-28T15:20:43Z")

</div>

This query should delete for example, connected components that have 10 nodes or less 🙂  
You will need the APOC plugin installed on the database.

```auto
MATCH (a)-[*]-(b)
WITH id(a) AS id, apoc.coll.sortText(apoc.coll.toSet(collect(DISTINCT b.id) + [a.id])) AS nodes_list
WITH DISTINCT nodes_list, size(nodes_list) AS size
WHERE size <= 10
WITH nodes_list
CALL apoc.periodic.iterate('
    MATCH (n)
    WHERE n.id IN $nodes_list
    RETURN n
    ', '
    DETACH DELETE n
    ', {batchSize:1000, iterateList:true, params:{nodes_list:nodes_list}}) YIELD batch, operations
RETURN 1

```

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 28, 2020, 9:00pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/7 "2020-08-28T21:00:33Z")

</div>

Thank you for your reply.  
That looks great. I managed to installed apoc, run the query, 1 is returned, so the query seems to be executed with success. But the nodes and edges are still there :

 ![8-28-2020 22-58-41 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/1/10cba24e8aa91bbbb924bd35a443caa39fa9b6e6.png)

I feel like this vodoo spell needs to be optimized ;-)

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 28, 2020, 9:31pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/8 "2020-08-28T21:31:50Z")

</div>

Can you tell me the labels of your nodes and their properties?

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 29, 2020, 5:01am UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/9 "2020-08-29T05:01:18Z")

</div>

Sure. Attached my nodes file (no properties, just a label). Is that the cause of the issue ?  
[Nodes.txt](https://community.neo4j.com/uploads/short-url/y5bqCZQtrr0WcuhxkvQNBcfVnZ.txt) (869 Bytes)

Best regards

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 29, 2020, 8:25am UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/10 "2020-08-29T08:25:05Z")

</div>

I created an id property on my examples, that's why I'm asking.  
Try this one, it use the Neo4j id:

```auto
MATCH (a)-[*]-(b)
WITH id(a) AS id, apoc.coll.sort(apoc.coll.toSet(collect(DISTINCT id(b)) + [id(a)])) AS nodes_list
WITH DISTINCT nodes_list, size(nodes_list) AS size
WHERE size <= 10
WITH nodes_list
CALL apoc.periodic.iterate('
    MATCH (n)
    WHERE id(n) IN $nodes_list
    RETURN n
    ', '
    DETACH DELETE n
    ', {batchSize:1000, iterateList:true, params:{nodes_list:nodes_list}}) YIELD batch, operations
RETURN 1

```

Regards,  
Cobra

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 29, 2020, 2:03pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/11 "2020-08-29T14:03:33Z")

</div>

It works like charm.  
I will try to understand this query.  
Thank you very much ! 🙂

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 29, 2020, 2:26pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/12 "2020-08-29T14:26:39Z")

</div>

Happy to hear 🙂

Don't hesitate if you have any trouble to understand my query 🙂

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 30, 2020, 8:33pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/13 "2020-08-30T20:33:35Z")

</div>

Hi,

Thank you for your feedback. I launch the query yesterday on a large database (5 million nodes, and 10 millions Edges) and the process is still running. So, I am not sure this way will fit my need.  
What I do with Gephi : There's the possibility to run stats for a specific set of data. By running the stat based on network components, you obtain a component ID for each network. Then, you can filter out the networks that are smaller than a certain size.The pb with Gephi is that he can't manage big data.  
Would it be possible to do more or less the same thing with Neo4j : First, obtaining some stats on the data and then filtering out unintersting data ?  
Another approach would be to obtain these stats an rather than deleting small networks, make a query to obtain the list of a specific Nodes for all networks greater than a certain size.  
I'm not suer I 'm very clear... 🙂  
Base on your knoledge what would be the best option to that with a large database ?

Best regards,

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 30, 2020, 8:55pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/14 "2020-08-30T20:55:52Z")

</div>

Hello @1113 ;)

First, did you use UNIQUE CONSTRAINTS to create your nodes?

Yeah, your way should be also possible on Neo4j, I will try tomorrow 🙂

Regards,  
Cobra

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 30, 2020, 9:41pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/15 "2020-08-30T21:41:10Z")

</div>

Hi Cobra,

I didn't use unique constraints to create the nodes.  
I will check the doc to determine how to do that.  
Looking forward to get your feedback 🙂

Best regards,

@1113 ;-)

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 31, 2020, 5:55am UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/16 "2020-08-31T05:55:48Z")

</div>

The UNIQUE CONSTRAINT should speed up the query, it's something to have when you work with Neo4j 🙂

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 31, 2020, 6:45am UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/17 "2020-08-31T06:45:59Z")

</div>

Hi, I added the UNIQUE CONSTRAINT on all the entities (all are unique) and relaunched the query.  
Let's see :-)  
Have a great day !

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 31, 2020, 6:51am UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/18 "2020-08-31T06:51:46Z")

</div>

Can you tell me which property is unique? Like this we can use this one in the query I gave you 🙂  
Thanks, you too!

---

<div class="post-metadata">

### Author: ![1113](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/1113/32/13464_2.png) [@1113](https://community.neo4j.com/u/1113)
#### Post date: [August 31, 2020, 1:58pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/19 "2020-08-31T13:58:07Z")

</div>

Hi,

In fact I use several Entitities : Item1, Item2, ... they don't have any properties except their Label and they are all unique.

Best regards,

---

<div class="post-metadata">

### Author: ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)
#### Post date: [August 31, 2020, 2:15pm UTC](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166/20 "2020-08-31T14:15:15Z")

</div>

Ok, so we will have to create a community for each size and tag each node with his community in order to delete them but I don't know if it will be faster. In your case, the problem is you don't have a unique property, that's why everything takes time I think 🙂

[Next page](https://community.neo4j.com/t/how-to-remove-connected-components-less-than-x-nodes/24166.md?page=2)
