Skip to content

[Question] Limiting index size #5

Answered by Stevenic
Stevenic asked this question in Q&A
Discussion options

You must be logged in to vote

Link to my original answer:

#3 (comment)

Here's the code sample I provided:

async function pruneIndex(index, max_items) {
   // check index against retention policy
   const stats = await index.getIndexStats();
   if (stats.items < max_items) {
      return;
   }

   // Remove oldest items first
   await index.beginUpdate();    
   try {
      const items = await index.listItems();
      while (items.length > max_items) {
         const item = items.unshif();
         await index.deleteItem(item.id);
      }
      await index.endUpdate();
   } catch (err) {
      await index.cancelUpdate();
      throw err;
   }
} 

Replies: 1 comment

Comment options

Stevenic
Jun 21, 2023
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by Stevenic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant