fix: clear linked comments, versions and attachments with old logs

Repost Item Valuation and BOM Update Log cleared old logs with a raw
delete on the parent table, orphaning timeline comments, versions,
attachments and other reference records.

Fixes #57237
This commit is contained in:
Mihir Kandoi
2026-07-17 21:24:08 +05:30
parent a31119353c
commit 1887825ce5
4 changed files with 73 additions and 18 deletions

View File

@@ -4,10 +4,37 @@ from contextlib import contextmanager
import frappe
from frappe import _
from frappe.utils import cstr
from frappe.utils import create_batch, cstr
from erpnext.utilities.activation import get_level
LOG_REFERENCE_FIELDS = {
"Comment": ("reference_doctype", "reference_name"),
"Version": ("ref_doctype", "docname"),
"ToDo": ("reference_type", "reference_name"),
"DocShare": ("share_doctype", "share_name"),
"View Log": ("reference_doctype", "reference_name"),
"Document Follow": ("ref_doctype", "ref_docname"),
"Notification Log": ("document_type", "document_name"),
}
def clear_logs_with_references(doctype, filters):
names = frappe.get_all(doctype, filters=filters, pluck="name")
for batch in create_batch(names, 1000):
attached_files = frappe.get_all(
"File",
filters={"attached_to_doctype": doctype, "attached_to_name": ("in", batch)},
pluck="name",
)
if attached_files:
frappe.delete_doc("File", attached_files, ignore_permissions=True)
for reference_doctype, (doctype_field, name_field) in LOG_REFERENCE_FIELDS.items():
frappe.db.delete(reference_doctype, {doctype_field: doctype, name_field: ("in", batch)})
frappe.db.delete(doctype, {"name": ("in", batch)})
def update_doctypes():
df = frappe.qb.DocType("DocField")