Skip to main content
Glama

truncate_collection

Delete all documents in a collection while preserving its schema. This process retrieves the schema, deletes the collection, and recreates it with the same structure, ensuring data is cleared without altering the schema design.

Instructions

Truncates a collection by deleting all documents but keeping the schema. Achieved by retrieving schema, deleting collection, and recreating it. Args: ctx (Context): The MCP context. collection_name (str): The name of the collection to truncate. Returns: str: A success or error message string.

Input Schema

NameRequiredDescriptionDefault
collection_nameYes

Input Schema (JSON Schema)

{ "properties": { "collection_name": { "title": "Collection Name", "type": "string" } }, "required": [ "collection_name" ], "title": "truncate_collectionArguments", "type": "object" }

Implementation Reference

  • main.py:518-570 (handler)
    The handler function for the 'truncate_collection' tool. It truncates a Typesense collection by backing up the schema, deleting the collection, and recreating it with the same schema to remove all documents while preserving the structure.
    async def truncate_collection(ctx: Context, collection_name: str) -> str: """ Truncates a collection by deleting all documents but keeping the schema. Achieved by retrieving schema, deleting collection, and recreating it. Args: ctx (Context): The MCP context. collection_name (str): The name of the collection to truncate. Returns: str: A success or error message string. """ if not collection_name: return "Error: collection_name parameter is required." client: typesense.Client = ctx.request_context.lifespan_context.client original_schema = None try: # 1. Retrieve the current schema (assuming sync) print(f"Truncating '{collection_name}': Retrieving schema...") original_schema = client.collections[collection_name].retrieve() # We only need the fields, name, and potentially other top-level settings for re-creation # Remove read-only fields like 'created_at', 'num_documents' before re-creating schema_to_recreate = { key: value for key, value in original_schema.items() if key in ['name', 'fields', 'default_sorting_field', 'token_separators', 'symbols_to_index', 'enable_nested_fields'] } # 2. Delete the collection (assuming sync) print(f"Truncating '{collection_name}': Deleting original collection...") client.collections[collection_name].delete() # 3. Recreate the collection with the original schema (assuming async) print(f"Truncating '{collection_name}': Recreating collection with schema...") await client.collections.create(schema_to_recreate) return f"Successfully truncated collection '{collection_name}'." except typesense.exceptions.ObjectNotFound: return f"Error during truncate: Collection '{collection_name}' not found (maybe already deleted?)." except typesense.exceptions.TypesenseClientError as e: error_message = f"Error during truncate operation on '{collection_name}': {e}" print(error_message) # Attempt to restore if deletion succeeded but recreation failed? # For now, just report the error stage. if original_schema and not client.collections[collection_name].exists(): # Check if deleted but not recreated error_message += " Original collection was deleted but recreation failed." return error_message except Exception as e: print(f"An unexpected error occurred during truncate for '{collection_name}': {e}") return f"An unexpected error occurred during truncate: {e}"

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/avarant/typesense-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server