microsoft/semantic-kernel · error · ServiceResourceNotFoundError

collection {collection_name} not found

Error message

collection {collection_name} not found

What it means

Raised as a ServiceResourceNotFoundError in MongoDBAtlasMemoryStore.remove when does_collection_exist returns False. Unlike upsert/get (which do not pre-check), the remove path explicitly guards: it refuses to delete from a non-existent collection.

Source

Thrown at python/semantic_kernel/connectors/memory_stores/mongodb_atlas/mongodb_atlas_memory_store.py:239

        """
        results = self.database[collection_name].find({MONGODB_FIELD_ID: {"$in": keys}})

        return [
            document_to_memory_record(result, with_embeddings) for result in await results.to_list(length=len(keys))
        ]

    async def remove(self, collection_name: str, key: str) -> None:
        """Removes a memory record from the data store. Does not guarantee that the collection exists.

        Args:
            collection_name (str): The name associated with a collection of embeddings.
            key (str): The unique id associated with the memory record to remove.

        Returns:
            None
        """
        if not await self.does_collection_exist(collection_name):
            raise ServiceResourceNotFoundError(f"collection {collection_name} not found")
        await self.database[collection_name].delete_one({MONGODB_FIELD_ID: key})

    async def remove_batch(self, collection_name: str, keys: list[str]) -> None:
        """Removes a batch of memory records from the data store. Does not guarantee that the collection exists.

        Args:
            collection_name (str): The name associated with a collection of embeddings.
            keys (List[str]): The unique ids associated with the memory records to remove.

        Returns:
            None
        """
        if not await self.does_collection_exist(collection_name):
            raise ServiceResourceNotFoundError(f"collection {collection_name} not found")
        deletes: list[DeleteOne] = [DeleteOne({MONGODB_FIELD_ID: key}) for key in keys]
        bulk_write_result = await self.database[collection_name].bulk_write(deletes, ordered=False)
        logger.debug("%s entries deleted", bulk_write_result.deleted_count)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Guard with await store.does_collection_exist(collection_name) before calling remove.
  2. Catch ServiceResourceNotFoundError and treat removal of a missing collection as a no-op.
  3. Verify the collection name and the target database.
  4. Note the asymmetry: remove_batch lacks this check, so prefer remove_batch if idempotent deletion is desired.

Example fix

// before
await store.remove('docs', key)  # ServiceResourceNotFoundError
// after
if await store.does_collection_exist('docs'):
    await store.remove('docs', key)
else:
    logging.info('Collection docs absent; skip remove')
Defensive patterns

Strategy: validation

Validate before calling

async def safe_remove(store, name: str, key: str) -> None:
    if await store.does_collection_exist(name):
        await store.remove(name, key)
    else:
        logging.info('Collection %s absent; skip remove', name)

Try / catch

from semantic_kernel.exceptions import ServiceResourceNotFoundError

try:
    await store.remove('docs', key)
except ServiceResourceNotFoundError:
    pass  # collection absent; nothing to remove

Prevention

When it happens

Trigger: Calling await store.remove('my_collection', key) when the MongoDB collection does not exist. Note that remove_batch does NOT have this guard (only remove does), so the asymmetry can cause confusion.

Common situations: Cleanup code targeting a collection already dropped or never created. Collection name typo. Running remove in an environment where create_collection was never called. Fresh database with no collections.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/3f5e1bd1d9dcb61d. Report an issue: GitHub.