# Most efficient way to Clone a Branch from a top level node

**URL:** <https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639>\
**Category:** Cypher\
**Created:** [December 5, 2018, 9:35am UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639 "2018-12-05T09:35:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johncrockeratwork](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/johncrockeratwork/32/1915_2.png) [@johncrockeratwork](https://community.neo4j.com/u/johncrockeratwork)\
**Post date:** [December 5, 2018, 9:35am UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639/1 "2018-12-05T09:35:29Z")

</div>

Hello

We have a requirement to clone a tree from a given root node into exactly the same tree structure from a newly created Root node. The cloned tree only has to traverse down to level "n" and only traverse certain relationships.

Does anyone have any suggestions for the most efficient way of doing this? Our initial experiments have taken somewhere in the region of "minutes" and have put a medium-large load on the server.

---

<div class="post-metadata">

**Author:** ![tim.hanssen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tim.hanssen/32/875_2.png) [@tim.hanssen](https://community.neo4j.com/u/tim.hanssen)\
**Post date:** [December 8, 2018, 3:00am UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639/2 "2018-12-08T03:00:59Z")

</div>

Hi!

Can you share your query are you are running it now? And tell me a bit more about the scope, how many nodes on avg ect.

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [December 8, 2018, 9:32pm UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639/3 "2018-12-08T21:32:47Z")

</div>

I answered a [similar question on Stack Overflow](https://stackoverflow.com/questions/53596520/how-to-duplicate-node-tree-in-neo4j) recently, are you the same one who asked that question?

If not, then my answer may be helpful for you.

If so, then it would help to know what's going on in the query, and I'd request you PROFILE your query and add the query plan (after expanding all elements of the plan) to your question.

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [February 4, 2019, 11:07am UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639/4 "2019-02-04T11:07:39Z")

</div>

There is also a apoc procedure for that:

```auto
apoc.refactor.cloneNodesWithRelationships

```

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 4, 2019, 12:17pm UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639/5 "2019-02-04T12:17:25Z")

</div>

I think this gets messy, as the cloned relationships attach to all outside nodes as well as to the original nodes, leaving an exercise for the caller on how to delete all the relationships between the clones and the originals.

I've added an [APOC feature request](https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1075) for a new procedure `apoc.refactor.cloneSubgraph()` that is aimed at this particular use case: cloning nodes and the relationships between them, only creating relationships between the clones and not to any of the originals, and allowing a means to define a replacement or standin for nodes when it comes to relationship creation. For this example, it could be used to clone a branch and attach the branch to a different root node instead of the old root (or instead of cloning the old root).

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [February 7, 2019, 10:11am UTC](https://community.neo4j.com/t/most-efficient-way-to-clone-a-branch-from-a-top-level-node/3639/6 "2019-02-07T10:11:59Z")

</div>

As an update, I have a PR open with a new APOC procedure for doing exactly this:

> <https://github.com/neo4j-contrib/neo4j-apoc-procedures/pull/1087>
>
> Fixes / implements feature request #1075 
> 
> Introduces apoc.refactor.cloneSubgr…aph for cloning and reanchoring a subgraph of nodes and relationships between them.
> 
> JUnit tests and overview documentation included (refactoring procs).
> 
> Requesting backport to 3.4 as well if possible.
