# Delete relationships of node which got deleted

**URL:** <https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151>\
**Category:** Java\
**Created:** [July 29, 2021, 8:44am UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151 "2021-07-29T08:44:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyash.ninan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyash.ninan/32/13257_2.png) [@lyash.ninan](https://community.neo4j.com/u/lyash.ninan)\
**Post date:** [July 29, 2021, 8:44am UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151/1 "2021-07-29T08:44:17Z")

</div>

I have reached a state where there was a piece of code which deleted the nodes without deleting the relationships of the same. Now I am frequently getting node with node id not found error. Is there a way I can delete such relationships and indexes and any other such entities which can stop the node not found error?

---

<div class="post-metadata">

**Author:** ![florent\_biville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/florent_biville/32/14205_2.png) [@florent\_biville](https://community.neo4j.com/u/florent_biville)\
**Post date:** [July 29, 2021, 8:59am UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151/2 "2021-07-29T08:59:30Z")

</div>

Neo4j normally prevents you from deleting a node if you haven't deleted its relationships in a previous or in the same transaction. The transaction should not commit in that case.  
Would you mind sharing the code at play here?

---

<div class="post-metadata">

**Author:** ![lyash.ninan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyash.ninan/32/13257_2.png) [@lyash.ninan](https://community.neo4j.com/u/lyash.ninan)\
**Post date:** [July 29, 2021, 9:12am UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151/3 "2021-07-29T09:12:49Z")

</div>

This is the error

rg.neo4j.graphdb.NotFoundException: Node 47643382 not found  
at org.neo4j.kernel.impl.factory.GraphDatabaseFacade.getNodeById(GraphDatabaseFacade.java:228)  
at org.neo4j.kernel.impl.factory.GraphDatabaseFacade$3.apply(GraphDatabaseFacade.java:526)  
at org.neo4j.kernel.impl.factory.GraphDatabaseFacade$3.apply(GraphDatabaseFacade.java:522)  
at org.neo4j.collection.primitive.PrimitiveLongCollections$14.next(PrimitiveLongCollections.java:746)  
at org.neo4j.helpers.collection.ResourceClosingIterator.next(ResourceClosingIterator.java:76)  
...  
Caused by: org.neo4j.kernel.api.exceptions.EntityNotFoundException: Unable to load NODE with id 47643382.  
... 76 common frames omitted

This piece of error is from below:

while (parentNodes.hasNext())  
{  
Node parentEntityNode = parentNodes.next(); - At this line.  
...  
}

Delete of the node code is:

try (Transaction tx = graphDb.beginTx())  
{  
boolean deleted = deleteNodeFromPlane(label, key, value);  
tx.success();  
}  
catch (Exception e)  
{  
String errMessage = "Error in deleting nodes";  
LOGGER.error(errMessage, e);  
throw new GraphException(errMessage, e);  
}

private boolean deleteNodeFromPlane(String label, String key, Object value)  
{  
Node node = graphDb.findNode(label, key, value);  
node.delete();  
return true;  
}

---

<div class="post-metadata">

**Author:** ![florent\_biville](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/florent_biville/32/14205_2.png) [@florent\_biville](https://community.neo4j.com/u/florent_biville)\
**Post date:** [July 29, 2021, 3:15pm UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151/4 "2021-07-29T15:15:29Z")

</div>

> [@lyash.ninan](#):
>
> while (parentNodes.hasNext())  
> {  
> Node parentEntityNode = parentNodes.next(); - At this line.  
> ...  
> }

Can you show the code around this as well?  
Just to see where `parentNodes` is defined and what the transactional context is.

---

<div class="post-metadata">

**Author:** ![lyash.ninan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyash.ninan/32/13257_2.png) [@lyash.ninan](https://community.neo4j.com/u/lyash.ninan)\
**Post date:** [July 29, 2021, 3:34pm UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151/5 "2021-07-29T15:34:35Z")

</div>

This is the whole flow of code with transaction flow in it.

public void updateMeta(Metadata metadata) throws Exception {

```
try (Transaction tx = graphDb.beginTx())
{
		saveMeta(products, company);
		tx.success();
}catch (Exception e)
{
	//Exception Handling
}

```

}

private void saveMeta(List products, String company) {  
try  
{

```
	Node tenantNode = graphDb.findNode(key,label,value);
	
	for (String product : products)
	{
		if (tenantNode.hasProperty(product))
		{
			
			persistMetaForProduct(tenantNode, product, company);
		}
	}
}
catch (Exception e)
{
	//exception handling
}

```

}

private void persistMetaForProduct(Node tenantNode, String product, String company)  
{

```
ResourceIterator<Node> parentNodes;
parentNodes = graphDb.findNodes(
		DynamicLabel.label(key,label,value);
while (parentNodes.hasNext())
{
	
	Node parentEntityNode = parentNodes.next();
	//other line of code	
}

```

}

---

<div class="post-metadata">

**Author:** ![lyash.ninan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyash.ninan/32/13257_2.png) [@lyash.ninan](https://community.neo4j.com/u/lyash.ninan)\
**Post date:** [July 29, 2021, 3:37pm UTC](https://community.neo4j.com/t/delete-relationships-of-node-which-got-deleted/42151/6 "2021-07-29T15:37:07Z")

</div>

Just to add another point is that, we have not added any transaction.failure() anywhere. we have always used try with resources and within that if no exception, transaction.success() as show in the above code.
