파일 삭제 시 관련 chunk 전부 제거
박수현 · (사)한국정보공학기술사회 · 2026
DELETE /files/<source>where 필터로 대상 지정🎯 목표: 파일 1개 삭제 시 관련 chunk 전부와 원본 파일까지 일괄 정리한다.
💡 색인 시 부여한 source 메타데이터가 삭제 기준이다. 메타데이터가 없으면 삭제 자체가 불가능하다.
def delete_document(source: str) -> bool:
store._collection.delete( # source 일치 chunk 전부
where={"source": source}
)
# 원본 파일도 제거
path = os.path.join("uploads", source)
if os.path.exists(path):
os.remove(path)
return True
_collection.delete(where=...) — 조건에 맞는 chunk 일괄 삭제📁 참고:
vectorstore.py의delete_document
@app.delete("/files/<path:source>")
def remove_file(source):
ok = delete_document(source)
if not ok:
return jsonify({"error": "삭제 실패"}), 404
return jsonify({"deleted": source})
<path:source> — 파일명에 /·.가 포함돼도 안전하게 수신🔑 REST 원칙대로 리소스 경로 + DELETE 메서드. 동일 API를 curl로도 호출 가능.
where 삭제로 완료where 삭제 → 다시 add (일관성 유지)🔑 삭제 후 재추가 패턴이 부분 수정의 기본 방식.
source 로 일괄 삭제_collection.delete(where={"source": ...})DELETE /files/<source> — REST 리소스 삭제