Skip to main content
Glama

delete

Permanently remove a database record by its ID, including related graph edges and triggering deletion events. This irreversible operation fails if foreign key constraints exist.

Instructions

Delete a specific record from the database by its ID.

This tool permanently removes a record from the database. Use with caution as this operation cannot be undone. The deletion will also:

  • Remove any graph edges (relations) connected to this record

  • Trigger any defined deletion events/hooks

  • Fail if the record is referenced by FOREIGN KEY constraints

Args: thing: The full record ID to delete in format "table:id" (e.g., "user:john", "product:laptop-123") namespace: Optional SurrealDB namespace override. If not provided, uses SURREAL_NAMESPACE env var. database: Optional SurrealDB database override. If not provided, uses SURREAL_DATABASE env var.

Returns: A dictionary containing: - success: Boolean indicating if deletion was successful - deleted: The ID of the deleted record - data: The deleted record data (if available) - error: Error message if deletion failed (only present on failure)

Examples: >>> await delete("user:john") {"success": true, "deleted": "user:john", "data": {"id": "user:john", "name": "John Doe"}}

>>> await delete("product:nonexistent")
{"success": true, "deleted": "product:nonexistent", "data": null}  # No error even if record didn't exist

Note: This operation is irreversible. Consider using soft deletes (status fields) for recoverable deletions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
thingYes
namespaceNo
databaseNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The primary handler for the 'delete' tool, registered via @mcp.tool(). Handles input validation, fetches deleted record data, calls repo_delete, and formats the response.
    async def delete(
        thing: str,
        namespace: Optional[str] = None,
        database: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        Delete a specific record from the database by its ID.
    
        This tool permanently removes a record from the database. Use with caution as this operation
        cannot be undone. The deletion will also:
        - Remove any graph edges (relations) connected to this record
        - Trigger any defined deletion events/hooks
        - Fail if the record is referenced by FOREIGN KEY constraints
    
        Args:
            thing: The full record ID to delete in format "table:id" (e.g., "user:john", "product:laptop-123")
            namespace: Optional SurrealDB namespace override. If not provided, uses SURREAL_NAMESPACE env var.
            database: Optional SurrealDB database override. If not provided, uses SURREAL_DATABASE env var.
    
        Returns:
            A dictionary containing:
            - success: Boolean indicating if deletion was successful
            - deleted: The ID of the deleted record
            - data: The deleted record data (if available)
            - error: Error message if deletion failed (only present on failure)
    
        Examples:
            >>> await delete("user:john")
            {"success": true, "deleted": "user:john", "data": {"id": "user:john", "name": "John Doe"}}
    
            >>> await delete("product:nonexistent")
            {"success": true, "deleted": "product:nonexistent", "data": null}  # No error even if record didn't exist
    
        Note: This operation is irreversible. Consider using soft deletes (status fields) for recoverable deletions.
        """
        try:
            ns, db = resolve_namespace_database(namespace, database)
    
            # Validate thing format
            if ":" not in thing:
                raise ValueError(f"Invalid record ID format: {thing}. Must be 'table:id'")
    
            logger.info(f"Deleting record {thing}")
    
            # Try to get the record first (optional, for returning deleted data)
            try:
                select_result = await repo_query(f"SELECT * FROM {thing}", namespace=ns, database=db)
                deleted_data = select_result[0] if select_result else None
            except Exception:
                deleted_data = None
    
            # Perform the deletion
            record_id = ensure_record_id(thing)
            await repo_delete(record_id, namespace=ns, database=db)
    
            return {
                "success": True,
                "deleted": thing,
                "data": deleted_data
            }
        except Exception as e:
            logger.error(f"Delete failed for {thing}: {str(e)}")
            raise Exception(f"Failed to delete {thing}: {str(e)}")
  • Helper function repo_delete that executes the actual DELETE operation on the SurrealDB connection.
    async def repo_delete(
        record_id: Union[str, RecordID],
        namespace: Optional[str] = None,
        database: Optional[str] = None,
    ):
        """Delete a record by record id.
    
        Args:
            record_id: The record ID to delete
            namespace: Optional namespace override (uses env var if not provided)
            database: Optional database override (uses env var if not provided)
    
        Returns:
            The deletion result
        """
        try:
            async with db_connection(namespace, database) as connection:
                return await connection.delete(record_id)
        except Exception as e:
            logger.exception(e)
            raise RuntimeError(f"Failed to delete record: {str(e)}")
  • Utility function to resolve namespace and database from tool parameters or environment variables, used by all tools including delete.
    def resolve_namespace_database(
        namespace: Optional[str] = None,
        database: Optional[str] = None,
    ) -> Tuple[Optional[str], Optional[str]]:
        """
        Resolve namespace and database values from parameters or environment variables.
    
        Args:
            namespace: Optional namespace parameter from tool call
            database: Optional database parameter from tool call
    
        Returns:
            Tuple of (resolved_namespace, resolved_database). Both will be None if using
            default pooled connection, or both will be strings if using override connection.
    
        Raises:
            ValueError: If namespace/database cannot be determined from either source
        """
        # Get values from env vars as fallback
        env_namespace = os.environ.get("SURREAL_NAMESPACE")
        env_database = os.environ.get("SURREAL_DATABASE")
    
        # Resolve final values
        final_namespace = namespace if namespace is not None else env_namespace
        final_database = database if database is not None else env_database
    
        # If both are from env vars (or both params are None), use pooled connection
        if namespace is None and database is None and env_namespace and env_database:
            return None, None  # Signal to use pooled connection
    
        # If either param is provided, we need both values resolved
        if final_namespace is None or final_database is None:
            missing = []
            if final_namespace is None:
                missing.append("namespace")
            if final_database is None:
                missing.append("database")
            raise ValueError(
                f"Missing required database configuration: {', '.join(missing)}. "
                "Either set SURREAL_NAMESPACE/SURREAL_DATABASE environment variables "
                "or provide namespace/database parameters in the tool call."
            )
    
        return final_namespace, final_database
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It thoroughly explains the irreversible nature, cascading effects (removing graph edges, triggering hooks), failure conditions (FOREIGN KEY constraints), and even includes edge-case behavior (no error if record doesn't exist). This provides comprehensive behavioral context beyond basic parameter documentation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (purpose, warnings, args, returns, examples, notes) and every sentence adds value. While comprehensive, it could be slightly more concise by integrating some details more tightly, but the information density is high and organization supports quick scanning.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a destructive mutation tool with no annotations, 3 parameters, and 0% schema coverage, this description is exceptionally complete. It covers purpose, behavioral consequences, all parameters, return values (though output schema exists), examples, warnings, and even suggests alternatives. Nothing essential is missing given the tool's complexity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Despite 0% schema description coverage, the description fully compensates by explaining all three parameters. It clarifies 'thing' as 'the full record ID in format "table:id"' with examples, and explains the optional 'namespace' and 'database' parameters with their default behaviors from environment variables. This adds substantial meaning beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with specific verb ('Delete') and resource ('a specific record from the database by its ID'). It distinguishes itself from sibling tools like 'create', 'update', and 'patch' by focusing on permanent removal rather than creation or modification.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context about when to use this tool ('permanently removes a record') and includes a cautionary note about irreversibility. It suggests an alternative ('Consider using soft deletes') but doesn't explicitly contrast with specific sibling tools like 'patch' or 'update' for partial modifications.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/lfnovo/surreal-mcp'

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