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.get_kitems(limit=100)[-1]

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)
[4]:
contact:
  name: Specimen preparation
  email: specimenpreparation@group.mail
  user_id: null

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 = {}
/app/dsms/knowledge/kitem.py:570: UserWarning: A flat dictionary was provided for custom properties.
                    Will be transformed into `KItemCustomPropertiesModel`.
  warnings.warn(

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

[7]:
dsms.commit()

See the changes:

[8]:
item
[8]:
kitem:
  name: Specimen-123
  id: 8ba20087-210d-482c-a847-6f80c20b7d51
  ktype_id: specimen
  in_backend: true
  slug: specimen123-8ba20087
  annotations: []
  attachments:
  - name: testfile.txt
    content: null
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts:
  - name: Specimen preparation
    email: specimenpreparation@group.mail
    user_id: null
  created_at: 2025-01-17 10:44:42.478274
  updated_at: 2025-01-17 10:51:55.054607
  external_links:
  - label: specimen-link
    url: http://specimens.org
  kitem_apps: []
  user_groups: []
  custom_properties:
    sections:
    - id: ida3c5c42685526
      name: Untitled Section
      entries:
      - id: id175f885f8dda58
        type: Slider
        label: Width
        value: 1
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: idef5a37328789f
        type: Slider
        label: Length
        value:
        - 0.1
        - 0.2
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: id54decb9641f9a
        type: Number
        label: Radius
        value: 10
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
  rdf_exists: true

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.