microsoft/semantic-kernel · error · VectorStoreOperationException

Container could not be deleted.

Error message

Container could not be deleted.

What it means

Raised when database_proxy.delete_container raises any exception during ensure_collection_deleted. Unlike creation (which only catches CosmosHttpResponseError), deletion catches all Exception, so any failure — HTTP error, network drop, client not connected — is wrapped as a VectorStoreOperationException with the original chained.

Source

Thrown at python/semantic_kernel/connectors/azure_cosmos_db.py:1048

        except CosmosHttpResponseError as e:
            raise VectorStoreOperationException("Failed to create container.") from e

    @override
    async def collection_exists(self, **kwargs) -> bool:
        container_proxy = await self._get_container_proxy(self.collection_name, **kwargs)
        try:
            await container_proxy.read(**kwargs)
            return True
        except CosmosHttpResponseError:
            return False

    @override
    async def ensure_collection_deleted(self, **kwargs) -> None:
        database_proxy = await self._get_database_proxy(**kwargs)
        try:
            await database_proxy.delete_container(self.collection_name)
        except Exception as e:
            raise VectorStoreOperationException("Container could not be deleted.") from e

    @override
    async def __aexit__(self, exc_type, exc_value, traceback) -> None:
        """Exit the context manager."""
        if self.managed_client:
            await self.cosmos_client.close()


# region: NoSQL Store


@release_candidate
class CosmosNoSqlStore(CosmosNoSqlBase, VectorStore):
    """A VectorStore implementation that uses Azure CosmosDB NoSQL as the backend storage."""

    def __init__(
        self,
        url: str | None = None,

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Check exc.__cause__ for the real status code (404 means already gone — often safe to treat as success).
  2. Verify the database/container name and that the client is connected before deleting.
  3. Wrap deletion so a 404/not-found is treated as idempotent success rather than an error.

Example fix

// before
await store.ensure_collection_deleted()
// after
try:
    await store.ensure_collection_deleted()
except VectorStoreOperationException as e:
    if getattr(e.__cause__, "status_code", None) == 404:
        pass  # already deleted, idempotent
    else:
        raise
Defensive patterns

Strategy: try-catch

Try / catch

from semantic_kernel.exceptions import VectorStoreOperationException

try:
    await store.ensure_collection_deleted()
except VectorStoreOperationException as e:
    status = getattr(getattr(e.__cause__, "status_code", None), "__int__", lambda: None)()
    if getattr(e.__cause__, "status_code", None) == 404:
        pass  # idempotent: already gone
    else:
        raise

Prevention

When it happens

Trigger: Calling ensure_collection_deleted (or delete_collection) when the container does not exist (depending on SDK behavior), when the client is not authenticated/connected, during a transient network failure, or when the principal lacks delete permission.

Common situations: Deleting a non-existent or already-deleted collection; network interruption mid-call; permission/RBAC mismatch; the underlying cosmos_client was never connected (managed_client path).

Related errors


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