#community-help

Effective Document Deletion Strategies in Typesense

TLDR Adrian asked about deleting potentially non-existent documents. Pantelis suggested using IDs for document deletion or queries within delete function. Adrian decided to use query-based deletions.

Powered by Struct AI
7
4mo
Solved
Join the chat
Aug 08, 2023 (4 months ago)
Adrian
Photo of md5-27ff63286c7b3dcb91085f39e910c437
Adrian
02:29 PM
If I want to delete a document that may or may not exist. Would you suggest explicitly checking for the Could not find a document with id: error and treating that as a non error case, or is there a better way to do so? Perhaps deleting by query would be a better approach
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
02:32 PM
what i did is i assigned an id to each document so i can query it by id and delete it or check if it exists. Checking by error seems to be dangerous in the sense that maybe tomorrow the error can change to something else.
Adrian
Photo of md5-27ff63286c7b3dcb91085f39e910c437
Adrian
02:38 PM
Good idea. When you say query for it do you mean a search query or the retrieve endpoint? I presumed the retrieve endpoint could have the same issue
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
02:43 PM
typesenseClient.collection('your-collection').documents.delete('your-document-id')
If it is not found then i am catching the error. The delete accepts idOrQuery. So if you have assigned an id before in each of your documents then typesense will find it and delete it. When i want to delete a set of documents i use a query inside delete, for instance: delete('submitted:=true') which is going to find all the submitted documents in your schema and delete them
Adrian
Photo of md5-27ff63286c7b3dcb91085f39e910c437
Adrian
02:47 PM
I see. Thanks for the input!
02:47
Adrian
02:47 PM
I think I will go with the approach of deleting by query for all documents
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
02:48 PM
yw, whatever suits your case better! 🙂