# DELETE all relationships before creating new ones

**URL:** <https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417>\
**Category:** General\
**Created:** [December 14, 2020, 5:20pm UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417 "2020-12-14T17:20:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrankR85](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/frankr85/32/13519_2.png) [@FrankR85](https://community.neo4j.com/u/FrankR85)\
**Post date:** [December 14, 2020, 5:20pm UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417/1 "2020-12-14T17:20:36Z")

</div>

We want to store a simple graph: (p:Person {id: 5)-[:LIKES]-\>(s:Song)

When we receive new information about the songs that a person likes, we want to

- first delete existing LIKE-relationships for this person
- create the new ones

The last part is easy:

```auto
foreach (song in $k.songdata |
    create (p)-[:LIKES]->(s:Song {title: k.title}))

```

For the first part we could use sth like

```auto
match (p:Person {id: k.id})-[:LIKES]->(s) detach delete (s)

```

But how can we combine these in one statement? We need to do this with plain Cypher, without apoc.

---

<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:** [December 14, 2020, 8:58pm UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417/2 "2020-12-14T20:58:52Z")

</div>

Hello @FrankR85 🙂

First, be aware it's a good thing to separate these queries, but here you have a way (moreover UNION should not be used like this):

```auto
MATCH (p:Person {id: k.id})-[r:LIKES]->(s)
DELETE (r)
UNION
FOREACH (song IN $k.songdata |
    CREATE (p)-[:LIKES]->(s:Song {title: k.title}))

```

You could also have a look at [MERGE](https://neo4j.com/docs/cypher-manual/current/clauses/merge/#query-merge-relationships).

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![FrankR85](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/frankr85/32/13519_2.png) [@FrankR85](https://community.neo4j.com/u/FrankR85)\
**Post date:** [December 15, 2020, 6:57am UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417/3 "2020-12-15T06:57:22Z")

</div>

Thank you for your reply!  
We would split the statement if we could, but we are using Neo4j streams and we noticed a massive degrade in throughput when we used apoc.cypher.runMany. It is up to 10x slower than a plain Cypher query.

That being said, I have some trouble getting your query to work within UNWIND.  
Running

```auto
UNWIND $events as event
MATCH (p:Person {id: event.id})-[r:LIKES]->(s)
DELETE (r)
UNION
FOREACH (song IN event.songdata |
    CREATE (p)-[:LIKES]->(s:Song {title: k.title}))

```

yields an error because **event** is unknown in the clause following the union. I just can't get it to work. 🙈

If you know of another possibility to split the statements, we would be glad to hear it.

Unfortunatly we haven't figured out yet the exact reason why apoc.cypher.runMany is so slow.

---

<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:** [December 15, 2020, 8:34am UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417/4 "2020-12-15T08:34:57Z")

</div>

Do you use a driver to send queries to your database?

```auto
UNWIND $events as event
MATCH (p:Person {id: event.id})-[r:LIKES]->(s)
DELETE (r)
UNION
UNWIND $events as event
FOREACH (song IN event.songdata |
    CREATE (p)-[:LIKES]->(s:Song {title: k.title}))

```

---

<div class="post-metadata">

**Author:** ![FrankR85](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/frankr85/32/13519_2.png) [@FrankR85](https://community.neo4j.com/u/FrankR85)\
**Post date:** [December 15, 2020, 9:03am UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417/5 "2020-12-15T09:03:03Z")

</div>

Thank you! This query works fine! 🕶

I don't know what you mean with the question about the driver. We use a [Cypher-Template](https://neo4j.com/labs/kafka/4.0/consumer/#_cypher_template) as Neo4j streams sink ingestion strategy.

---

<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:** [December 15, 2020, 9:12am UTC](https://community.neo4j.com/t/delete-all-relationships-before-creating-new-ones/30417/6 "2020-12-15T09:12:30Z")

</div>

Nice:)

I was talking about this:

> **[Create applications - Neo4j Documentation](https://neo4j.com/docs/create-applications/)**
>
> Create applications - Neo4j Documentation
