How to delete records?

Records can be deleted with record.delete(), which will permanently remove them from your database.

When it comes to records of File and Collection, they are “moved into trash” when you first call record.delete().

  • Trashed records are invisible in the UI and excluded from the query results, see visibility faq.

  • If a record is already in the trash or permanent=True is passed, calling record.delete() triggers permanent delete.

  • During permanent deletion of a record, its artifact in storage is also deleted unless it has a semantic key.

Setup

Install the lamindb Python package:

pip install lamindb
!lamin init --storage test-delete
💡 connected lamindb: testuser1/test-delete
import lamindb as ln
import pandas as pd
💡 connected lamindb: testuser1/test-delete
artifact = ln.Artifact.from_df(pd.DataFrame({"a": [1, 2], "b": [3, 4]}), description="mydf")
artifact.save()
❗ no run & transform get linked, consider calling ln.track()
Artifact(uid='k9N9m3rUgB7S54jFtu4C', description='mydf', suffix='.parquet', type='dataset', accessor='DataFrame', size=2240, hash='RzXuf-54uJK6W56QmZJquQ', hash_type='md5', visibility=1, key_is_virtual=True, created_by_id=1, storage_id=1, updated_at='2024-07-26 14:36:14 UTC')
ln.Artifact.df()
uid version description key suffix type accessor size hash hash_type n_objects n_observations visibility key_is_virtual storage_id transform_id run_id created_by_id updated_at
id
1 k9N9m3rUgB7S54jFtu4C None mydf None .parquet dataset DataFrame 2240 RzXuf-54uJK6W56QmZJquQ md5 None None 1 True 1 None None 1 2024-07-26 14:36:14.467476+00:00

Trash an artifact

artifact.delete()
❗ moved artifact to trash (visibility = -1)

No longer visible:

ln.Artifact.df()
uid version description key suffix type accessor size hash hash_type n_objects n_observations visibility key_is_virtual storage_id transform_id run_id created_by_id updated_at
id

But the artifact still exists in the database, you can find it by not filtering for visibility:

ln.Artifact.filter(visibility=None).df()
uid version description key suffix type accessor size hash hash_type n_objects n_observations visibility key_is_virtual storage_id transform_id run_id created_by_id updated_at
id
1 k9N9m3rUgB7S54jFtu4C None mydf None .parquet dataset DataFrame 2240 RzXuf-54uJK6W56QmZJquQ md5 None None -1 True 1 None None 1 2024-07-26 14:36:14.520242+00:00

You can restore an artifact from trash:

artifact.restore()
ln.Artifact.df()
uid version description key suffix type accessor size hash hash_type n_objects n_observations visibility key_is_virtual storage_id transform_id run_id created_by_id updated_at
id
1 k9N9m3rUgB7S54jFtu4C None mydf None .parquet dataset DataFrame 2240 RzXuf-54uJK6W56QmZJquQ md5 None None 1 True 1 None None 1 2024-07-26 14:36:14.566840+00:00

Permanent delete

Calling artifact.delete on a trashed artifact triggers a permanent delete dialog. You can pass permanent=True to auto-confirm the deletion.

artifact.delete(permanent=True)

Now its gone in the database:

ln.Artifact.filter(visibility=None).df()
uid version description key suffix type accessor size hash hash_type n_objects n_observations visibility key_is_virtual storage_id transform_id run_id created_by_id updated_at
id