langgenius / langgenius/dify

Deleting a disabled multimodal segment leaves attachment records orphaned

Open
#41,457 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
156k
Forks
24.6k
Avg merge
20h 50m
Merged PRs (30d)
586

Description

## Summary

Deleting a disabled segment from a multimodal dataset removes the `DocumentSegment` row but skips the asynchronous cleanup that owns the segment's attachment records.

This leaves `SegmentAttachmentBinding` and `UploadFile` rows orphaned. The same durable cleanup is also skipped when the cleanup task runs after the parent document becomes disabled, archived, or leaves the completed indexing state.

## Steps to reproduce

1. Create a multimodal knowledge base and index a document containing an attachment-backed segment.
2. Disable the segment.
3. Delete the disabled segment through either the Console API or Dataset Service API.
4. Inspect `segment_attachment_bindings` and the corresponding `upload_files` row.

## Actual behavior

The segment row is deleted and the document word count is committed, but attachment bindings and upload-file metadata remain.

## Expected behavior

Deleting a segment should remove its durable attachment records regardless of whether the segment or its parent document is currently eligible for index operations.

## Root cause

`SegmentService.delete_segment()` only dispatches `delete_segment_from_index_task` when `segment.enabled` is true. In addition, the task returns before attachment cleanup when the document is disabled, archived, or not fully indexed. `SegmentAttachmentBinding.segment_id` has no database foreign-key cascade, so deleting the segment row cannot clean these records automatically.

## Notes

This is separate from #41399. That issue covers attachment blobs not being removed after the cleanup task reaches its attachment branch. This issue occurs earlier: the cleanup task is not dispatched or returns before any attachment database cleanup runs.

Contributor guide

Open the contributing guide

Research direction

Start at SegmentService.delete_segment() and follow delete_segment_from_index_task, checking where disabled segments and ineligible parent documents exit before attachment cleanup. Reproduce through the Console API or Dataset Service API, then verify that segment_attachment_bindings and the corresponding upload_files row are removed for each described state.

Written by the indexing model from the issue text.

Assessment

Domain
api, backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
67/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.