function delete_notification
Deletes a notification node from a Neo4j graph database by its unique identifier (UID).
/tf/active/vicechatdev/CDocs/utils/notifications.py
1080 - 1102
simple
Purpose
This function removes a notification record from the database by matching its UID and performing a DETACH DELETE operation to remove the node and all its relationships. It's used in notification management systems to clean up notifications that are no longer needed, have been dismissed by users, or need to be removed as part of data maintenance operations. The function includes error handling and logging to track deletion failures.
Source Code
def delete_notification(notification_uid: str) -> bool:
"""
Delete a notification.
Args:
notification_uid: UID of notification
Returns:
Boolean indicating success
"""
try:
db.run_query(
"""
MATCH (n:Notification {UID: $notification_uid})
DETACH DELETE n
""",
{"notification_uid": notification_uid}
)
return True
except Exception as e:
logger.error(f"Error deleting notification {notification_uid}: {e}")
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
notification_uid |
str | - | positional_or_keyword |
Parameter Details
notification_uid: A string representing the unique identifier (UID) of the notification to be deleted. This should be a valid UID that exists in the database as a property of a Notification node. The value is typically a UUID string format.
Return Value
Type: bool
Returns a boolean value: True if the notification was successfully deleted from the database, False if an exception occurred during the deletion process. Note that the function returns True even if no matching notification was found (the query executes successfully but affects zero nodes).
Dependencies
CDocslogging
Required Imports
import logging
from CDocs import db
Usage Example
import logging
from CDocs import db
# Initialize logger
logger = logging.getLogger(__name__)
# Example 1: Delete a notification by UID
notification_uid = "550e8400-e29b-41d4-a716-446655440000"
success = delete_notification(notification_uid)
if success:
print(f"Notification {notification_uid} deleted successfully")
else:
print(f"Failed to delete notification {notification_uid}")
# Example 2: Delete multiple notifications
notification_uids = [
"550e8400-e29b-41d4-a716-446655440000",
"660e8400-e29b-41d4-a716-446655440001"
]
for uid in notification_uids:
result = delete_notification(uid)
print(f"Deletion of {uid}: {'Success' if result else 'Failed'}")
Best Practices
- Always verify that the notification_uid exists before calling this function if you need to distinguish between 'not found' and 'deletion error' scenarios
- Consider implementing a soft delete mechanism (marking as deleted rather than removing) if you need to maintain audit trails or allow recovery
- Use this function within a transaction context if you need to ensure atomicity with other database operations
- Log the notification_uid before deletion if you need to track what was deleted for audit purposes
- Be aware that DETACH DELETE removes all relationships, so ensure this is the desired behavior for your use case
- Consider implementing authorization checks before deletion to ensure the caller has permission to delete the notification
- The function returns True even if no notification was found - implement additional checks if you need to verify the notification existed
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function delete_all_notifications 77.6% similar
-
function delete_node 75.8% similar
-
function mark_notification_read 68.2% similar
-
function count_user_notifications 66.5% similar
-
function create_notification 64.6% similar