Skip to main content
Answer

Cascading delete of instance

  • July 1, 2024
  • 3 replies
  • 65 views

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. 

Best answer by Andre Alves

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

3 replies

Andre Alves
MVP
Forum|alt.badge.img+14
  • MVP
  • Answer
  • July 4, 2024

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


Dilini Fernando
Seasoned Practitioner
Forum|alt.badge.img+2

Hi @Erik Wiker,

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


  • Committed
  • April 2, 2025

This answer seems out of date. `client.relationships.list(target=instance_id)` does not have the `target` argument. I can use `target_external_ids`, but that still gives me nothing (empty response). 

Moreover,  `client.edges.list(target=instance_id)` does not exist altogether. 

I think the world has moved on to `client.data_modeling`. Can you show how to do this using `client.data_modeling.instances.list` apis?