Delete custom entity using storage API

Hi, I have a question on how to delete data using the storage API without having access to the entity’s key.
We created a custom entity called “contractor” which contains four fields: employeename, email, phone, country. When we were adding new contractors, we also generated an UUID to use as key.

Problem is, this key doesn’t seem to be accessible after it’s set. Using contractor.key returns undefined. How can we delete our entities without having access to the key?

Custom entities have a default index called by-key that you can use to query all items:

const result = await storage.entity("contractor").query().index("by-key").getMany();

This will return the first page of contractors, including their keys.

You could also include the key in contractorData before storing it, which will make it easier to delete the item afterwards.

Excellent! Forgot about that index. I had to add .query() to your line, but I was able to pull out the keys and delete them manually. And thank you for your suggestion.

Marking this as solved