Skip to content

Conversation

Subash-Mohan
Copy link
Contributor

@Subash-Mohan Subash-Mohan commented Aug 13, 2025

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.

  • This PR should be backported (make sure to check that the backport attempt succeeds)
  • [Optional] Override Linear Check

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.

  • Bug Fixes
  • Deleting a folder or file now reliably removes all related data and triggers connector cleanup tasks.
  • Added error handling and logging for deletion failures.

@Subash-Mohan Subash-Mohan requested a review from a team as a code owner August 13, 2025 14:47
Copy link

vercel bot commented Aug 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Project Deployment Preview Comments Updated (UTC)
internal-search Ready Preview Comment Aug 13, 2025 3:02pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a 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

Edit Code Review Bot Settings | Greptile

Comment on lines 339 to 340
f"cc_pair={file.cc_pair.id}"
)
Copy link
Contributor

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

Suggested change
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'}"
)

Comment on lines 267 to 271
client_app.send_task(
OnyxCeleryTask.CHECK_FOR_CONNECTOR_DELETION,
priority=OnyxCeleryPriority.HIGH,
kwargs={"tenant_id": tenant_id},
)
Copy link
Contributor

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

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a 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}"
Copy link
Contributor

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),
 ) -&gt; 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>
Suggested change
f"cc_pair={file.cc_pair.id}"
f"cc_pair={file.cc_pair.id if file.cc_pair else 'None'}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant