How to delete an object

I want to delete an object based on an id attribute. here is my code:

def delete_hierarchy_from_tiledb(uri, ctx, hierarchy_id):
    print(f"Deleting hierarchy with ID at uri: {hierarchy_id} at {uri}")    
    try:
        with tiledb.open(uri, mode='r', ctx=ctx) as A:
            # Assuming the 'ids' are stored under a dimension or attribute named 'id'
            all_ids = A[:]['id']
            if hierarchy_id.encode('utf-8') in all_ids:
                del A[hierarchy_id]
                return True
            else:
                return False
    except Exception as e:
        print(f"Error while attempting to delete hierarchy: {str(e)}")
        return False

BUt when it is called I get an error:

Error while attempting to delete hierarchy: Subscript deletion not supported by SparseArray

Hi @Tim_Turner,

We’ll take a look at whether it is feasible to implement __delitem__ on the array object to make this more Pythonic.

For now, please see https://docs.tiledb.com/main/how-to/arrays/writing-arrays/deleting#python

Best,
Isaiah

I changed the code to have an isActive flag that we set to false if we are deleting. Then all reads filter out the objects that are not active. When I do that though I get this error:

“SparseArray is not opened in read or delete mode”

I opened tiledb for writing like this:
with tiledb.open(uri, “w”, ctx=ctx) as A:

I am simply setting a value on the object in the array.

A[index] = {‘isActive’: np.array([False], dtype=bool)}

Hi @Tim_Turner,

Could you please share a complete, reduced example?

Thanks,
Isaiah

I switched to use the delete method in the article you provided and ditched the isActive flag method.

But it does beg the question of whether / how to “write” to update an existing object because it does not seem to want to allow me to write a new value to an attribute.