# Clone Subgraph and link nodes back to original node with apoc clone

**URL:** <https://community.neo4j.com/t/clone-subgraph-and-link-nodes-back-to-original-node-with-apoc-clone/63216>\
**Category:** General\
**Tags:** apoc, cypher\
**Created:** [July 10, 2023, 9:47am UTC](https://community.neo4j.com/t/clone-subgraph-and-link-nodes-back-to-original-node-with-apoc-clone/63216 "2023-07-10T09:47:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas\_stuempfi](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/thomas_stuempfi/32/289_2.png) [@thomas\_stuempfi](https://community.neo4j.com/u/thomas_stuempfi)\
**Post date:** [July 10, 2023, 9:47am UTC](https://community.neo4j.com/t/clone-subgraph-and-link-nodes-back-to-original-node-with-apoc-clone/63216/1 "2023-07-10T09:47:01Z")

</div>

Hello all,  
i need to clone a subgraph and link the individual nodes of the cloned subgraph back to the original nodes.  
Example  
(n1:A)-[r:demorel]-\>(n2:B)  
(n1)-[r:demorel]-\>(n3:C)  
(n2)-[r:demorel]-\>(n4:D)  
(n2)-[r:demorel]-\>(n5:E)

now clone this subgraph to (can be done with apoc...)  
(n11:A1)-[r:demorel]-\>(n21:B1)  
(n11)-[r:demorel]-\>(n31:C1)  
(n21)-[r:demorel]-\>(n41:D1)  
(n21)-[r:demorel]-\>(n51:E1)

and create rels  
(n11)-[r:original]-\>(n1)  
(n21)-[r:original]-\>(n2)  
(n31)-[r:original]-\>(n3)  
(n41)-[r:original]-\>(n4)  
(n51)-[r:original]-\>(n5)

or like so

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/e/e/ee863a59525cdd76a5fa9a2dfcd74f357b48e09e.png)

regards  
Thomas

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [July 16, 2023, 4:44pm UTC](https://community.neo4j.com/t/clone-subgraph-and-link-nodes-back-to-original-node-with-apoc-clone/63216/2 "2023-07-16T16:44:12Z")

</div>

It looks like the apoc.refactor.cloneNodes procedure returns the 'id' of the node being cloned as 'input' and the cloned node as 'output'. As such, you could search for the node corresponding to 'input' using its given 'id' and create a relationships between the matched node and the 'output' node.

Here is an example showing how to do it:

```auto
create (f:Foo{name:'Foo'}),(b:Bar{name:'Bar'})
with f, b
CALL apoc.refactor.cloneNodes([f,b])
YIELD input, output, error
match(x where id(x)=input)
create(x)-[:HAS_CLONE]->(output)
return id(x), x, id(output), output

```

![Screen Shot 2023-07-16 at 12.40.18 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/0/2016340c83423ddc12f36b51bbbc684da13c319f.png)

![Screen Shot 2023-07-16 at 12.43.32 PM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/e/1/e1bf8334e897b7522fbacfd5d6ac7e513e10becd.png)
