Auto Deletion in doctype

Hi All

How to auto-delete data inside doctype after certain timeline.

import frappe
from frappe.model.document import Document

class CustomDoctype(Document):
    @staticmethod
    def clear_old_logs(days=None):
        days = days or 30
        from frappe.query_builder import Interval
        from frappe.query_builder.functions import Now
        table = frappe.qb.DocType("CustomDoctype")
        frappe.db.delete(
            table,
            filters=(
                (table.modified < (Now() - Interval(days=days))) & 
                (table.status == "Completed")
            )
        )

Steps:

  1. Goto your CustomDoctype.py and add this code , also you can add filters whatever you want, set deletion days or set it from Log Setting.
  2. Next go to Log Setting doctype and add your CustomDoctype in Logs To Clear child table and set Clear Logs After Days

For Reference check this Logging

Hi Vipul Kumar

I am using ERPnext 13.34.2 version.

add clear_old_logs function in your .py file and migrate, then check whether your custom doctype is coming here or not

It’s working, Thanks a lot.