If I want to delete a document that may or may not...
# community-help
a
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
p
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.
a
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
p
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
a
I see. Thanks for the input!
I think I will go with the approach of deleting by query for all documents
p
yw, whatever suits your case better! 🙂