delete_table_tool
Remove specific tables from Google Sheets while preserving other content. Specify the spreadsheet, sheet, and table names to delete tables and their data permanently.
Instructions
Delete tables from Google Sheets.
This tool removes specified tables from a sheet while preserving other content.
The table structure and data will be permanently deleted.
Args:
spreadsheet_name: Name of the spreadsheet
sheet_name: Name of the sheet containing the tables
table_names: List of table names to delete
Returns:
JSON string with success status and deletion details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sheet_name | Yes | The name of the sheet containing the tables | |
| spreadsheet_name | Yes | The name of the Google Spreadsheet | |
| table_names | Yes | List of table names to delete (e.g., ['Project Tracker', 'Customer Data', 'Sales Report']) |
Implementation Reference
- gsheet_mcp_server/server.py:280-302 (registration)Registers the delete_table_tool with @mcp.tool() decorator. Defines input parameters with descriptions and validation using Pydantic Field. Delegates execution to the core delete_table_handler function.@mcp.tool() def delete_table_tool( spreadsheet_name: str = Field(..., description="The name of the Google Spreadsheet"), sheet_name: str = Field(..., description="The name of the sheet containing the tables"), table_names: List[str] = Field(..., description="List of table names to delete (e.g., ['Project Tracker', 'Customer Data', 'Sales Report'])") ) -> str: """ Delete tables from Google Sheets. This tool removes specified tables from a sheet while preserving other content. The table structure and data will be permanently deleted. Args: spreadsheet_name: Name of the spreadsheet sheet_name: Name of the sheet containing the tables table_names: List of table names to delete Returns: JSON string with success status and deletion details """ sheets_service, drive_service = _get_google_services() return delete_table_handler(drive_service, sheets_service, spreadsheet_name, sheet_name, table_names)
- Executes the core logic for deleting tables: validates table names, retrieves spreadsheet ID, sheet ID, and table IDs using helper functions, constructs 'deleteTable' batch requests, performs batchUpdate via Sheets API, and returns formatted JSON response with details.def delete_table_handler( drive_service, sheets_service, spreadsheet_name: str, sheet_name: str, table_names: List[str] ) -> str: """ Delete tables from Google Sheets. Args: drive_service: Google Drive service instance sheets_service: Google Sheets service instance spreadsheet_name: Name of the spreadsheet sheet_name: Name of the sheet containing the tables table_names: List of table names to delete Returns: str: Success message with deletion details """ try: # Validate inputs if not table_names or len(table_names) == 0: return compact_json_response({ "success": False, "message": "At least one table name is required." }) # Validate table names validated_table_names = [] invalid_table_names = [] for table_name in table_names: if not table_name or table_name.strip() == "": invalid_table_names.append({"name": table_name, "error": "Table name cannot be empty"}) continue validated_name = table_name.strip() validated_table_names.append(validated_name) if invalid_table_names: error_messages = [f"'{item['name']}': {item['error']}" for item in invalid_table_names] return compact_json_response({ "success": False, "message": f"Invalid table names: {'; '.join(error_messages)}", "invalid_table_names": invalid_table_names }) if not validated_table_names: return compact_json_response({ "success": False, "message": "No valid table names provided after validation." }) # Check for duplicate table names in the list seen_names = set() duplicate_names = [] for table_name in validated_table_names: if table_name in seen_names: duplicate_names.append(table_name) else: seen_names.add(table_name) if duplicate_names: return compact_json_response({ "success": False, "message": f"Duplicate table names in list: {', '.join(duplicate_names)}" }) # Get spreadsheet ID spreadsheet_id = get_spreadsheet_id_by_name(drive_service, spreadsheet_name) if not spreadsheet_id: return compact_json_response({ "success": False, "message": f"Spreadsheet '{spreadsheet_name}' not found." }) # Get sheet ID sheet_ids = get_sheet_ids_by_names(sheets_service, spreadsheet_id, [sheet_name]) sheet_id = sheet_ids.get(sheet_name) if sheet_id is None: return compact_json_response({ "success": False, "message": f"Sheet '{sheet_name}' not found in spreadsheet '{spreadsheet_name}'." }) # Get table IDs table_ids = get_table_ids_by_names(sheets_service, spreadsheet_id, sheet_name, validated_table_names) # Filter out tables that don't exist existing_table_ids = [] existing_table_names = [] non_existent_tables = [] for table_name in validated_table_names: table_id = table_ids.get(table_name) if table_id is not None: existing_table_ids.append(table_id) existing_table_names.append(table_name) else: non_existent_tables.append(table_name) if not existing_table_ids: return compact_json_response({ "success": False, "message": "No valid tables found to delete." }) # Create delete requests delete_requests = [] for table_id in existing_table_ids: delete_requests.append({ "deleteTable": { "tableId": table_id } }) # Execute batch delete response = sheets_service.spreadsheets().batchUpdate( spreadsheetId=spreadsheet_id, body={"requests": delete_requests} ).execute() # Extract response information replies = response.get("replies", []) deleted_count = len(replies) response_data = { "success": True, "spreadsheet_name": spreadsheet_name, "sheet_name": sheet_name, "deleted_table_names": existing_table_names, "tables_deleted": deleted_count, "message": f"Successfully deleted {deleted_count} table(s) from '{sheet_name}'" } # Add information about non-existent tables if non_existent_tables: response_data["non_existent_tables"] = non_existent_tables response_data["message"] += f" (Skipped {len(non_existent_tables)} non-existent table(s))" return compact_json_response(response_data) except HttpError as error: return compact_json_response({ "success": False, "message": f"Google Sheets API error: {str(error)}" }) except Exception as e: return compact_json_response({ "success": False, "message": f"Error deleting tables: {str(e)}" })