function delete_all_notifications
Deletes all notifications or only read notifications for a specific user from a Neo4j graph database.
/tf/active/vicechatdev/CDocs/utils/notifications.py
1104 - 1130
simple
Purpose
This function provides a way to clean up user notifications in a Neo4j database. It can either delete all notifications associated with a user or selectively delete only those marked as read. This is useful for notification management features, allowing users to clear their notification history or remove only acknowledged notifications. The function uses Cypher queries to match and delete notification nodes connected to a user node via HAS_NOTIFICATION relationships.
Source Code
def delete_all_notifications(user_uid: str, read_only: bool = False) -> bool:
"""
Delete all notifications for a user.
Args:
user_uid: UID of user
read_only: Whether to delete only read notifications
Returns:
Boolean indicating success
"""
try:
query = """
MATCH (u:User {UID: $user_uid})-[:HAS_NOTIFICATION]->(n:Notification)
"""
if read_only:
query += "WHERE n.read = true "
query += "DETACH DELETE n"
db.run_query(query, {"user_uid": user_uid})
return True
except Exception as e:
logger.error(f"Error deleting notifications for user {user_uid}: {e}")
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_uid |
str | - | positional_or_keyword |
read_only |
bool | False | positional_or_keyword |
Parameter Details
user_uid: String identifier (UID) of the user whose notifications should be deleted. This should match the UID property of a User node in the Neo4j database. Expected to be a unique identifier string, likely a UUID or similar format.
read_only: Boolean flag that determines deletion scope. When True, only notifications with read=true property are deleted. When False (default), all notifications for the user are deleted regardless of read status. Defaults to False for complete notification cleanup.
Return Value
Type: bool
Returns a boolean value indicating the success or failure of the deletion operation. Returns True if the Cypher query executes successfully (even if no notifications were found to delete). Returns False if an exception occurs during the database operation, with the error logged for debugging.
Dependencies
CDocslogging
Required Imports
import logging
from CDocs import db
Usage Example
# Delete all notifications for a user
user_id = "user-123-abc-456"
success = delete_all_notifications(user_id)
if success:
print("All notifications deleted successfully")
else:
print("Failed to delete notifications")
# Delete only read notifications
success = delete_all_notifications(user_id, read_only=True)
if success:
print("Read notifications deleted successfully")
else:
print("Failed to delete read notifications")
Best Practices
- Always check the return value to ensure the deletion was successful before updating UI or application state
- Use read_only=True when you want to preserve unread notifications, which is common in notification management UIs
- Ensure the user_uid exists in the database before calling this function to avoid unnecessary database queries
- Consider implementing a confirmation dialog before calling this function with read_only=False, as it permanently deletes all notifications
- The function uses DETACH DELETE which removes the notification nodes and their relationships, ensuring no orphaned relationships remain
- Monitor logs for errors as failures are logged with the user_uid for debugging purposes
- This function does not return the count of deleted notifications; consider modifying if you need that information
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function delete_notification 77.6% similar
-
function count_user_notifications 72.8% similar
-
function get_user_notifications 72.0% similar
-
function mark_notification_read 67.8% similar
-
function delete_node 66.8% similar