Solved

Cascading delete of instance


Hi!

Is there a way to enable cascading deletes for instances for both direct relations and edges? 

Edit: I posted in the wrong group, but i don’t think i can delete. 

icon

Best answer by Andre Alves 4 July 2024, 17:02

View original

2 replies

Userlevel 4
Badge +6

Hi @Erik Wiker!

As far I know Cognite CDF does not natively support cascading deletes for instances through both direct relations and edges. To achieve cascading deletes, you would typically need to implement custom logic within your application or python sciprt as follow.

 

from cognite.client import CogniteClient

def delete_instance_and_related(client, instance_id):
# Find all relationships and edges related to the instance
relationships = client.relationships.list(target=instance_id)
edges = client.edges.list(target=instance_id)

# Recursively delete related instances
for relationship in relationships:
delete_instance_and_related(client, relationship.source_id)

for edge in edges:
delete_instance_and_related(client, edge.source_id)

# Delete the instance itself
client.instances.delete(instance_id)

# Initialize the Cognite Client
client = CogniteClient()

# Call the function with the ID of the instance to delete
delete_instance_and_related(client, instance_id)

For detailed information you can go to
https://cognite-sdk-python.readthedocs-hosted.com/en/latest/data_modeling.html#cognite.client.data_classes.data_modeling.instances.Edge

Userlevel 4
Badge +2

Hi @Erik Wiker,

We are following up to see whether you're satisfied with the responses you've received? 

Reply