From 8d172d6139f59c93d96758130a13925375f2f8b4 Mon Sep 17 00:00:00 2001 From: Dev Khant Date: Wed, 22 Jan 2025 13:18:26 +0530 Subject: [PATCH] Update delete_users (#2166) --- docs/platform/quickstart.mdx | 31 ++++++++++ mem0/client/main.py | 106 ++++++++++++++++++++++++++++++++--- 2 files changed, 129 insertions(+), 8 deletions(-) diff --git a/docs/platform/quickstart.mdx b/docs/platform/quickstart.mdx index 0c4d2389..f6f53db8 100644 --- a/docs/platform/quickstart.mdx +++ b/docs/platform/quickstart.mdx @@ -1566,6 +1566,37 @@ client.delete_users() {'message': 'All users, agents, and runs deleted.'} ``` +Delete specific user or agent or app or run. + +```python Python +# Delete specific user +client.delete_users(user_id="alex") + +# Delete specific agent +#client.delete_users(agent_id="travel-assistant") + +# Delete specific app +#client.delete_users(app_id="travel-app") + +# Delete specific run +#client.delete_users(run_id="travel-run") +``` + +```javascript JavaScript +client.delete_users({ user_id: "alex" }) + .then(result => console.log(result)) + .catch(error => console.error(error)); +``` + +```bash cURL +curl -X DELETE "https://api.mem0.ai/v1/entities/?user_id=alex" \ + -H "Authorization: Token your-api-key" +``` + +```json Output +{'message': 'Entity deleted successfully.'} +``` + ### 4.8 Reset Client diff --git a/mem0/client/main.py b/mem0/client/main.py index 452f0ef4..6130405d 100644 --- a/mem0/client/main.py +++ b/mem0/client/main.py @@ -304,16 +304,61 @@ class MemoryClient: return response.json() @api_error_handler - def delete_users(self) -> Dict[str, str]: - """Delete all users, agents, or sessions.""" + def delete_users( + self, + user_id: Optional[str] = None, + agent_id: Optional[str] = None, + app_id: Optional[str] = None, + run_id: Optional[str] = None, + ) -> Dict[str, str]: + """Delete specific entities or all entities if no filters provided. + + Args: + user_id: Optional user ID to delete specific user + agent_id: Optional agent ID to delete specific agent + app_id: Optional app ID to delete specific app + run_id: Optional run ID to delete specific run + + Returns: + Dict with success message + + Raises: + ValueError: If specified entity not found + APIError: If deletion fails + """ params = self._prepare_params() entities = self.users() - for entity in entities["results"]: + + # Filter entities based on provided IDs using list comprehension + to_delete = [ + entity + for entity in entities["results"] + if (user_id and entity["type"] == "user" and entity["name"] == user_id) + or (agent_id and entity["type"] == "agent" and entity["name"] == agent_id) + or (app_id and entity["type"] == "app" and entity["name"] == app_id) + or (run_id and entity["type"] == "run" and entity["name"] == run_id) + ] + + # If filters provided but no matches found, raise error + if not to_delete and (user_id or agent_id or app_id or run_id): + raise ValueError("No entity found with the provided ID.") + # If no filters provided, delete all entities + elif not to_delete: + to_delete = entities["results"] + + # Delete entities and check response immediately + for entity in to_delete: response = self.client.delete(f"/v1/entities/{entity['type']}/{entity['id']}/", params=params) response.raise_for_status() - capture_client_event("client.delete_users", self) - return {"message": "All users, agents, and sessions deleted."} + capture_client_event( + "client.delete_users", self, {"user_id": user_id, "agent_id": agent_id, "app_id": app_id, "run_id": run_id} + ) + return { + "message": "Entity deleted successfully." + if (user_id or agent_id or app_id or run_id) + else "All users, agents, apps and runs deleted." + } @api_error_handler def reset(self) -> Dict[str, str]: @@ -664,14 +709,59 @@ class AsyncMemoryClient: return response.json() @api_error_handler - async def delete_users(self) -> Dict[str, str]: + async def delete_users( + self, + user_id: Optional[str] = None, + agent_id: Optional[str] = None, + app_id: Optional[str] = None, + run_id: Optional[str] = None, + ) -> Dict[str, str]: + """Delete specific entities or all entities if no filters provided. + + Args: + user_id: Optional user ID to delete specific user + agent_id: Optional agent ID to delete specific agent + app_id: Optional app ID to delete specific app + run_id: Optional run ID to delete specific run + + Returns: + Dict with success message + + Raises: + ValueError: If specified entity not found + APIError: If deletion fails + """ params = self.sync_client._prepare_params() entities = await self.users() - for entity in entities["results"]: + + # Filter entities based on provided IDs using list comprehension + to_delete = [ + entity + for entity in entities["results"] + if (user_id and entity["type"] == "user" and entity["name"] == user_id) + or (agent_id and entity["type"] == "agent" and entity["name"] == agent_id) + or (app_id and entity["type"] == "app" and entity["name"] == app_id) + or (run_id and entity["type"] == "run" and entity["name"] == run_id) + ] + + # If filters provided but no matches found, raise error + if not to_delete and (user_id or agent_id or app_id or run_id): + raise ValueError("No entity found with the provided ID.") + # If no filters provided, delete all entities + elif not to_delete: + to_delete = entities["results"] + + # Delete entities and check response immediately + for entity in to_delete: response = await self.async_client.delete(f"/v1/entities/{entity['type']}/{entity['id']}/", params=params) response.raise_for_status() + capture_client_event("async_client.delete_users", self.sync_client) - return {"message": "All users, agents, and sessions deleted."} + return { + "message": "Entity deleted successfully." + if (user_id or agent_id or app_id or run_id) + else "All users, agents, apps and runs deleted." + } @api_error_handler async def reset(self) -> Dict[str, str]: