function clear_user_uploaded_documents
Removes all uploaded documents associated with a specific user from the application state in a thread-safe manner.
/tf/active/vicechatdev/vice_ai/complex_app.py
224 - 228
simple
Purpose
This function is designed to clean up user-specific document data from the application's in-memory state. It uses a lock to ensure thread-safe deletion of a user's uploaded documents from a shared dictionary structure. This is typically used when a user logs out, when clearing session data, or as part of a cleanup routine to free memory.
Source Code
def clear_user_uploaded_documents(user_email):
"""Clear all uploaded documents for a user"""
with app_state['locks']['uploaded_documents']:
if user_email in app_state['uploaded_documents']:
del app_state['uploaded_documents'][user_email]
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
Parameter Details
user_email: String representing the user's email address, used as the key to identify and remove the user's uploaded documents from the app_state dictionary. Expected to be a valid email string that matches an existing key in app_state['uploaded_documents'].
Return Value
This function returns None (implicit). It performs an in-place deletion operation on the app_state dictionary and does not return any value.
Dependencies
threading
Required Imports
from threading import Lock
Usage Example
from threading import Lock
# Initialize app_state (typically done at application startup)
app_state = {
'locks': {
'uploaded_documents': Lock()
},
'uploaded_documents': {
'user@example.com': ['doc1.pdf', 'doc2.txt'],
'another@example.com': ['doc3.docx']
}
}
# Clear documents for a specific user
clear_user_uploaded_documents('user@example.com')
# After execution, app_state['uploaded_documents'] will be:
# {'another@example.com': ['doc3.docx']}
# Calling with non-existent user is safe (no error)
clear_user_uploaded_documents('nonexistent@example.com')
Best Practices
- Always ensure app_state is properly initialized before calling this function to avoid KeyError exceptions
- The function safely handles cases where the user_email does not exist in the dictionary (no exception is raised)
- This function is thread-safe due to the lock acquisition, making it suitable for multi-threaded Flask applications
- Consider calling this function during user logout, session expiration, or as part of periodic cleanup tasks
- The lock is automatically released even if an exception occurs within the with block
- Be aware that this only removes references from app_state; actual file deletion (if needed) must be handled separately
- For production systems, consider logging the deletion operation for audit purposes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function remove_uploaded_document 88.0% similar
-
function remove_uploaded_document_v1 85.4% similar
-
function get_user_uploaded_documents 78.5% similar
-
function remove_user_document 78.4% similar
-
function cleanup_old_documents 68.4% similar