#community-help

Typesense Cloud Issue: Unable to Delete Documents by ID

TLDR Ellen had trouble using Typesense Cloud's filter_by option to delete documents by id. Jason solved this by suggesting to remove double-quotes around IDs in filter_by clause.

Powered by Struct AI
+11
smile1
12
11mo
Solved
Join the chat
Oct 07, 2022 (11 months ago)
Ellen
Photo of md5-47fa9c35475086c618c51bce38eafc37
Ellen
01:28 AM
Hello! 👋 I’m using typesense cloud and running into an issue deleting documents using the filter_by option. For whatever reason, I can’t seem to get it to work when filtering by the id field. The same syntax seems to work when I use a different field. Will post an example in the thread.
01:30
Ellen
01:30 AM
this returns a num_deleted of 0 and does not remove the documents:
this.client.collections("collection").documents().delete({
      filter_by:
        'id:=["id1", "id2", "id3"]',
    });
01:32
Ellen
01:32 AM
this query using a different field successfully removes the documents:
this.client.collections("collection").documents().delete({
      filter_by:
          'name:=["name1", "name2"]',
    });
01:33
Ellen
01:33 AM
I also see the same behavior when I try the deletions from the typesense cloud UI
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:54 AM
👋 What version of Typesense are you on?
Ellen
Photo of md5-47fa9c35475086c618c51bce38eafc37
Ellen
04:02 AM
v0.23.1
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:05 AM
I just tried a simple example of adding a document and then deleting by the id field in the filter_by clause and it works for me. Can you please post a small reproduceable code snippet that I can try?
+11
Ellen
Photo of md5-47fa9c35475086c618c51bce38eafc37
Ellen
09:06 PM
Yes, let me know if this helps!
   const collectionName = 'delete-test-collection';

    // create collection
    const schema: CollectionCreateSchema = {
      name: collectionName,
      fields: [
        { name: 'id', type: 'string' },
        { name: 'name', type: 'string' },
        { name: 'createdAt', type: 'int64' },
        { name: '.*', type: 'auto' },
      ],
      default_sorting_field: 'createdAt',
    };

    await this.client.collections().create(schema);
    const documents = [
      {
        id: 'a620b2c0-7b19-4d38-813c-b26efe37ccce',
        name: 'doc1',
        createdAt: 1626895838564,
      },
      {
        id: 'cbf07f65-18d3-455f-aab1-4fd98c21a740',
        name: 'doc2',
        createdAt: 1626837810820,
      },
      {
        id: '6aeb7d7a-9d9e-4849-a2d6-b7af75725ea7',
        name: 'doc3',
        createdAt: 1626883734635,
      },
    ];
    await this.client
      .collections(collectionName)
      .documents()
      .import(documents, { action: 'upsert' });

    const deleteByIdRes = await this.client
      .collections(collectionName)
      .documents()
      .delete({
        filter_by:
          'id:=["a620b2c0-7b19-4d38-813c-b26efe37ccce", "cbf07f65-18d3-455f-aab1-4fd98c21a740"]',
      });

    console.log(`${deleteByIdRes.num_deleted} documents deleted by id`);

    const deleteByNameRes = await this.client
      .collections(collectionName)
      .documents()
      .delete({
        filter_by: 'name:=["doc1", "doc2"]',
      });

    console.log(`${deleteByNameRes.num_deleted} documents deleted by name`);
09:07
Ellen
09:07 PM
this is the output I get:
0 documents deleted by id
2 documents deleted by name
Oct 11, 2022 (11 months ago)
Ellen
Photo of md5-47fa9c35475086c618c51bce38eafc37
Ellen
04:42 PM
Kishore Nallan just wondering if you have any thoughts on this?
Oct 12, 2022 (11 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:50 PM
Ellen Thank you for sharing that detailed code-snippet, makes it much more easy to debug 🙏

In the filter_by clause, the IDs, should not be surrounded by double-quotes.

If I remove those double-quotes, it works as expected:

  const deleteByIdRes = await client
    .collections(collectionName)
    .documents()
    .delete({
      filter_by:
        'id:=[a620b2c0-7b19-4d38-813c-b26efe37ccce, cbf07f65-18d3-455f-aab1-4fd98c21a740]',
    })```
2 documents deleted by id
0 documents deleted by name```
Ellen
Photo of md5-47fa9c35475086c618c51bce38eafc37
Ellen
05:39 PM
awesome thank you! knew it had to be something like that 😅
smile1