-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix mydocuments deletion #5191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix mydocuments deletion #5191
Conversation
…ror handling and logging
…er document management
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Summary
This PR fixes a critical issue in the MyDocuments deletion process where user files weren't being completely removed from both the database and file storage. The changes implement comprehensive cleanup logic across three key areas:
Backend Deletion Logic Enhancement: The user documents API (backend/onyx/server/user_documents/api.py
) has been significantly restructured to handle complete deletion workflows. For both folder and file deletions, the new implementation follows a proper sequence: first marking connector credential pairs as DELETING
, then cleaning up all database associations (including Persona__UserFile
and Persona__UserFolder
junction tables), committing the transaction, and finally removing files from the file store. The code includes proper error handling with rollback mechanisms to maintain data consistency.
Background Task Processing Fix: The connector deletion background task (backend/onyx/background/celery/tasks/connector_deletion/tasks.py
) now includes user file connectors by adding include_user_files=True
to the get_connector_credential_pairs()
call. Previously, the background cleanup process was ignoring user file connector credential pairs, meaning they would be marked for deletion but never actually processed.
Frontend Upload Prevention: The document list component (web/src/app/chat/my-documents/[id]/components/DocumentList.tsx
) now prevents file uploads in the "Recent Documents" view by conditionally hiding the upload section when the folder ID matches the special constant RECENT_DOCUMENTS_FOLDER_ID = -1
.
These changes work together to ensure that when users delete documents through the UI, all associated data is properly cleaned up: database records are removed with proper cascading, files are deleted from storage, and background tasks immediately process the connector credential pair deletions.
Confidence score: 3/5
- This PR addresses critical cleanup logic but contains potential issues with error handling and logging that could cause runtime problems
- Score reflects complex deletion logic that spans multiple systems with some implementation concerns around file store error handling and potential null pointer exceptions
- Pay close attention to the delete_file function logging statement and file store error handling in the backend API file
3 files reviewed, 2 comments
f"cc_pair={file.cc_pair.id}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Potential null pointer exception - file.cc_pair
could be None here since you only check if file.cc_pair:
earlier, but the file object may have been modified after deletion
f"cc_pair={file.cc_pair.id}" | |
) | |
logger.info( | |
f"create_deletion_attempt_for_connector_id - running check_for_connector_deletion: " | |
f"cc_pair={file.cc_pair.id if file.cc_pair else 'None'}" | |
) |
client_app.send_task( | ||
OnyxCeleryTask.CHECK_FOR_CONNECTOR_DELETION, | ||
priority=OnyxCeleryPriority.HIGH, | ||
kwargs={"tenant_id": tenant_id}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Background task is triggered regardless of whether any cc_pairs actually exist. Consider only sending the task when cc_pair_count > 0
to avoid unnecessary work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cubic analysis
1 issue found across 3 files • Review in cubic
React with 👍 or 👎 to teach cubic. You can also tag @cubic-dev-ai
to give feedback, ask questions, or re-run the review.
|
||
logger.info( | ||
f"create_deletion_attempt_for_connector_id - running check_for_connector_deletion: " | ||
f"cc_pair={file.cc_pair.id}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file.cc_pair is dereferenced without a None-check; if the file being deleted has no connector-credential pair, this AttributeError will bubble up and cause the endpoint to return 500 instead of 200.
Prompt for AI agents
Address the following comment on backend/onyx/server/user_documents/api.py at line 339:
<comment>file.cc_pair is dereferenced without a None-check; if the file being deleted has no connector-credential pair, this AttributeError will bubble up and cause the endpoint to return 500 instead of 200.</comment>
<file context>
@@ -220,16 +294,56 @@ def delete_file(
db_session: Session = Depends(get_session),
) -> MessageResponse:
user_id = user.id if user else None
+ tenant_id = get_current_tenant_id()
file = (
db_session.query(UserFile)
.filter(UserFile.id == file_id, UserFile.user_id == user_id)
.first()
)
</file context>
f"cc_pair={file.cc_pair.id}" | |
f"cc_pair={file.cc_pair.id if file.cc_pair else 'None'}" |
Description
This pull request implements robust and consistent deletion logic for user folders and files, ensuring that both database records and file storage are cleaned up properly. It also triggers background tasks to handle connector credential pair deletions immediately after user-initiated deletions. Additionally, the frontend is updated to prevent file uploads in the "Recent Documents" view.
Linear issue link -> https://linear.app/danswer/issue/DAN-2322/mydocuments-deletion-process-isnt-removing-the-associated-files-and
How Has This Been Tested?
Tested from UI
Backporting (check the box to trigger backport action)
Note: You have to check that the action passes, otherwise resolve the conflicts manually and tag the patches.
Summary by cubic
Improved deletion logic for user folders and files to ensure both database records and file storage are cleaned up, and connector credential pairs are removed right after user-initiated deletions. The frontend now blocks file uploads in the "Recent Documents" view.