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.

[5]:
from dsms import DSMS

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

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

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

[7]:
item = dsms["5cb3a95e-4aac-434d-81ba-7c242fd245aa"]

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:

[8]:
item.attachments.pop(0)
item.annotations.pop(0)
item.external_links.pop(0)
item.contacts.pop(0)
[8]:
contact:
  name: Specimen preparation
  email: specimenpreparation@group.mail

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

[9]:
item.affiliations = []

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

[10]:
dsms.add(item)
dsms.commit()

See the changes:

[11]:
item
[11]:
kitem:
  id: 5cb3a95e-4aac-434d-81ba-7c242fd245aa
  name: Specimen-123
  ktype_id: specimen
  slug: specimen123-5cb3a95e
  annotations: []
  attachments:
  - name: testfile.txt
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts:
  - name: Specimen preparation
    email: specimenpreparation@group.mail
  created_at: 2025-08-13 14:29:01.826691
  updated_at: 2025-08-13 14:30:26.915234
  external_links:
  - label: specimen-link
    url: http://specimens.org
  apps: []
  user_groups: []
  custom_properties:
    content:
      sections:
      - id: idb424123144cdd8
        name: Untitled Section
        entries:
        - id: id6c76bbffe7ca78
          type: Number
          label: Width
          value: 1
          measurementUnit:
            iri: http://qudt.org/vocab/unit/MilliM
            label: Millimetre
            symbol: null
            namespace: http://qudt.org/vocab/unit
          relationMapping: null
          required: false
        - id: id717d07130a7618
          type: Slider
          label: Length
          value:
          - 0.1
          - 0.2
          measurementUnit:
            iri: http://qudt.org/vocab/unit/MilliM
            label: Millimetre
            symbol: null
            namespace: http://qudt.org/vocab/unit
          relationMapping: null
          required: false
  rdf_exists: true
  contexts: []

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:

[12]:
del dsms[item]

Commit the changes:

[13]:
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.