4. Deleting KItems with the SDK

4. Deleting KItems with the SDK#

In this tutorial we see how to delete new Kitems and their properties.

4.1. Setting up#

Before you run this tutorial: make sure to have access to a DSMS-instance of your interest, alongwith with installation of this package and have establised access to the DSMS through DSMS-SDK (refer to Connecting to DSMS)

Now let us import the needed classes and functions for this tutorial.

[1]:
from dsms import DSMS, KItem

Now source the environmental variables from an .env file and start the DSMS-session.

[2]:
dsms = DSMS(env=".env")

Then lets see the Kitem we are interested in to remove.

[3]:
item = dsms.kitems[-1]
print(item)
KItem(

        name = Machine-1,

        id = dd091666-a7c9-4b3b-8832-910bdec5c63c,

        ktype_id = testing-machine,

        in_backend = True,

        slug = machine-1-dd091666,

        annotations = [
                {
                        iri: www.machinery.org/,
                        name: ,
                        namespace: www.machinery.org,
                        description: None
                }
        ],

        attachments = [
                {
                        name: testfile.txt
                }
        ],

        linked_kitems = [],

        affiliations = [
                {
                        name: machine-team
                }
        ],

        authors = [
                {
                        user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
                }
        ],

        avatar_exists = False,

        contacts = [
                {
                        name: machinesupport,
                        email: machinesupport@group.mail,
                        user_id: None
                }
        ],

        created_at = 2024-08-19 18:12:11.338394,

        updated_at = 2024-08-19 18:12:11.338394,

        external_links = [
                {
                        label: machine-link,
                        url: http://machine.org/
                }
        ],

        kitem_apps = [],

        summary = None,

        user_groups = [
                {
                        name: machinegroup,
                        group_id: 123
                }
        ],

        custom_properties = {
                Producer: Machinery GmBH,
                Location: A404,
                Model Number: Bending Test Machine No 777
        },

        dataframe = None,

        rdf_exists = False
)

4.2. Deletion of KItems and their properties#

We can also remove properties from the KItem without deleting the KItem itself.

For the list-like properties, we can use the standard list-methods from basic Python again (e.g. pop, remove, etc. or the del-operator).

For the other, non-list-like properties, we can simply use the attribute-assignment again.

When we only want single parts of the properties in the KItem, we can do it like this:

[4]:
item.attachments.pop(0)
item.annotations.pop(0)
item.external_links.pop(0)
item.contacts.pop(0)
item.user_groups.pop(0)
[4]:
{
                        name: machinegroup,
                        group_id: 123
                }

However, we can also reset the entire property by setting it to e.g. an empty list again:

[5]:
item.affiliations = []

We can delete the custom properties by setting the property to an empty dict:

[6]:
item.custom_properties = {}

Send the changes to the DSMS with the commit-method:

[8]:
dsms.commit()

See the changes:

[11]:
item
[11]:
KItem(

        name = Machine-1,

        id = dd091666-a7c9-4b3b-8832-910bdec5c63c,

        ktype_id = testing-machine,

        in_backend = True,

        slug = machine-1-dd091666,

        annotations = [],

        attachments = [],

        linked_kitems = [],

        affiliations = [
                {
                        name: machine-team
                }
        ],

        authors = [
                {
                        user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
                }
        ],

        avatar_exists = False,

        contacts = [
                {
                        name: machinesupport,
                        email: machinesupport@group.mail,
                        user_id: None
                }
        ],

        created_at = 2024-08-19 18:12:11.338394,

        updated_at = 2024-08-19 18:12:11.338394,

        external_links = [
                {
                        label: machine-link,
                        url: http://machine.org/
                }
        ],

        kitem_apps = [],

        summary = None,

        user_groups = [],

        custom_properties = {
                id: dd091666-a7c9-4b3b-8832-910bdec5c63c,
                content: {}
        },

        dataframe = None,

        rdf_exists = False
)

However, we can also delete the whole KItem from the DSMS by applying the del-operator to the dsms-object with the individual KItem-object:

[9]:
del dsms[item]

Commit the changes:

[10]:
dsms.commit()

Now to check if the particular kitem was removed, we can do this by using the command: dsms.kitems or by logging into the frontend dsms instance.