cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

Delete relationships of node which got deleted

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?

5 REPLIES 5

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?

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;
}

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

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	
}

}

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.