Skip to main content
Answer

Delete root asset along with its children

  • July 27, 2023
  • 5 replies
  • 122 views

Forum|alt.badge.img+2

I was trying to check SDK creating an asset hierarchy along with children (16). I created it in a improper structure and wanted to delete all of them and redo it from scratch. 

When i try deleting it, i am getting an error.   

client.assets.delete(i.id)
CogniteNotFoundError: Not found: [{'id': 1035393670500031}]The API Failed to process some items.

But when i see the list for the parent id, I am to list all of the ids completely

assets_subtree = client.assets.list(asset_subtree_ids=[3043982824375333])
for i in assets_subtree:
    if i.name != 'LPM_YT_MODEL':
        print(i.id,i.name,i.description)

 

Best answer by Jason Dressel

@eashwar11, You can specific via the SDK to recursively delete your asset tree: https://cognite-sdk-python.readthedocs-hosted.com/en/latest/core_data_model.html#delete-assets

client.assets.delete(id=[3043982824375333], recursive=True)
-Jason

5 replies

Forum|alt.badge.img
  • Seasoned Practitioner
  • Answer
  • July 28, 2023

@eashwar11, You can specific via the SDK to recursively delete your asset tree: https://cognite-sdk-python.readthedocs-hosted.com/en/latest/core_data_model.html#delete-assets

client.assets.delete(id=[3043982824375333], recursive=True)
-Jason


Forum|alt.badge.img+2
  • Author
  • July 28, 2023

Thanks for the inputs @Jason Dressel . It helped!

 

Please could you guide on how to add the new creation of asset and hierarchy with a dataset that I already created. When I run the code, it creates the assets and hierarchy but doesn't update them all in my dataset. Dataset is still empty. 

# Code to create the LP -Classes and store the meta-data

import pandas as pd
from cognite.client import CogniteClient
from cognite.client.data_classes import Asset

csv_file = "lpm_asset_hierarchy.csv"
df = pd.read_csv(csv_file)

client = CogniteClient()

def create_asset(asset_name, description, dataset_id,lims_prefix=None, weight=None,pid = None):
metadata = {
"LIMS_Prefix": lims_prefix if pd.notna(lims_prefix) else 'NA',
"Weight": weight if pd.notna(weight) else 0
}
asset = client.assets.create(
Asset(name=asset_name, description=description, metadata=metadata,parent_id=pid)
)
return asset

dataset_id = 6572524964198398
root_asset = create_asset("LPM_YT_MODEL", "Root Asset", dataset_id)




for index, row in df.iterrows():
asset_name = row["LPM_class_code"]
description = row["Description"]
lims_prefix = row["LIMS_Prefix"]
weight = row["Weight"]

create_asset(asset_name, description, dataset_id, lims_prefix, weight,root_asset.id)

print("Assets with metadata created successfully!")

 

 


Forum|alt.badge.img

Assuming you have the ID of your data set, it can be specified on asset instantiation:

a1 = Asset(name=.., data_set_id=123)

 

Another tip is the create_hierarchy method which simplifies the creation of asset hierarchies (will insert in correct topological order for you). It also supports upserting 👌


Forum|alt.badge.img+2
  • Author
  • July 28, 2023

Thanks @Håkon V. Treider . Is there a way to delete the dataset I created in SDK?

In the script above, I manually created a dataset from UI and then used the ID to add in the asset hierarchy script. 

I want to delete the dataset and create a new dataset in the script itself (with name and description) and then attach all the assets to the newly created dataset. 

I am unable to delete the dataset that i created manually. Please help.


roman.chesnokov
Seasoned Practitioner

@eashwar11 you can only archive datasets, there is no such possibility to remove them. This is due to the fact that data access rights can be determined by the dataset, and when it is deleted, a situation may arise when it is impossible to delete the data.