The Deleted Document Doctype in Frappe serves as a temporary holding place or a "recycle bin" for documents that have been deleted from the system. When a user deletes a document, a record is created in "Deleted Document" storing a snapshot of the original document's data. This allows for auditing of deletions and provides a mechanism for restoring accidentally deleted documents.
- To keep a record of all documents deleted from the system.
- To store the data of the deleted document at the time of deletion.
- To allow System Managers to review deleted documents.
- To provide a facility to restore deleted documents.
- To help in maintaining an audit trail of document lifecycles, including deletions.
deleted_document.json: Defines the schema (fields, properties, permissions) of the Deleted Document Doctype.deleted_document.py: Contains the server-side Python controller logic, primarily for the restoration process and for clearing old deleted document records.deleted_document.js: Handles client-side JavaScript for the Deleted Document form, providing "Restore" or "Open" buttons.deleted_document_list.js: Customizes the list view for Deleted Documents.test_deleted_document.py: Contains unit tests for the restoration logic.
This JSON file outlines the structure for each Deleted Document record.
- Key Fields:
deleted_name(Data, In List View, Read Only): The original name (ID) of the document that was deleted. This is also thetitle_fieldfor this Doctype.deleted_doctype(Data, In List View, Read Only): The Doctype of the document that was deleted (e.g., "Sales Order", "Item").data(Code, Read Only): This is a crucial field that stores a JSON string representation of the entire deleted document's data at the moment it was deleted.restored(Check, Default: 0, In List View, Read Only): A flag (0 or 1) indicating whether this deleted document has subsequently been restored.new_name(Read Only): If the document was restored, this field will store the name (ID) of the newly created (restored) document. This is important because the original name might be unavailable or the document might be restored with a modified name.
in_create: 1: This property ensures that users cannot manually create "Deleted Document" records through the standard UI. These records are generated exclusively by the system when a document is deleted.- Permissions:
- Only users with the "System Manager" role have permissions to
readthese records,deletethem (which means permanently removing the deleted document's record from this log), andexportthem. - There are no
createorwritepermissions for any role, as these records are system-managed and generally immutable after creation (except for therestoredandnew_namefields updated by the system upon restoration).
- Only users with the "System Manager" role have permissions to
- Other Properties:
row_format: "Compressed" - For a more compact list view of deleted documents.track_changes: 1 - Changes to the Deleted Document records themselves (e.g., whenrestoredstatus changes) are versioned.
- Module: "Core"
The Python controller handles the restoration of documents and the cleanup of old deleted records.
DeletedDocument(Document)Class:no_feed_on_delete = True: Prevents the system from creating an Activity Log entry when a "Deleted Document" record itself is permanently purged.clear_old_logs(days=180)(Static Method):- Provides a utility to permanently delete "Deleted Document" records that are older than a specified number of days (defaulting to 180 days).
- This is important for database maintenance to prevent the
tabDeleted Documenttable from growing indefinitely. This can be scheduled as a regular background job.
restore(name, alert=True)(Whitelisted Function):- The core function responsible for restoring a document.
name: The name of the "Deleted Document" record to be restored.- It checks if the document has already been marked as
restored. - Deserializes the JSON data stored in the
datafield back into a Frappe document object. - Attempts to
insert()the document.- Handles Workflow/Status Issues: If an error like
frappe.DocstatusTransitionErroroccurs (e.g., trying to insert a document that was previously Submitted or Cancelled), it resets thedocstatusto 0 (Draft) and clears any workflow state field before re-attempting the insertion. This ensures documents are restored in a modifiable state.
- Handles Workflow/Status Issues: If an error like
- Adds a comment to the newly restored document indicating its origin.
- Updates the original "Deleted Document" record by setting
restored = 1and populatingnew_namewith the ID of the restored document.
bulk_restore(docnames)(Whitelisted Function):- Allows System Managers to restore multiple deleted documents in a single operation from the list view.
- It iterates through the provided list of "Deleted Document" names, calls the
restore()function for each, and provides progress updates. - It categorizes the results into restored, already restored (invalid), and failed attempts.
This script provides the user interface elements on the "Deleted Document" form.
frappe.ui.form.on("Deleted Document", { ... });:refresh: function (frm):- If
frm.doc.restoredis true (the document has been restored):- It adds an "Open" button. Clicking this button navigates the user to the form view of the restored document (using
frm.doc.deleted_doctypeandfrm.doc.new_name).
- It adds an "Open" button. Clicking this button navigates the user to the form view of the restored document (using
- If
frm.doc.restoredis false (the document has not been restored):- It adds a "Restore" button. Clicking this button calls the server-side
restoremethod. - On successful restoration, the form is reloaded (
frm.reload_doc()) to reflect the updated status (e.g., the "Restore" button changes to "Open").
- It adds a "Restore" button. Clicking this button calls the server-side
- If
- When a user with appropriate permissions deletes a document (e.g., a "Task", "Sales Invoice") in Frappe:
- The system automatically creates a new record in the "Deleted Document" Doctype.
- This record stores the original Doctype, original name, and a JSON snapshot of the deleted document's data.
- A System Manager can later navigate to the "Deleted Document" list view to see all such records.
- They can open a specific "Deleted Document" record.
- The
datafield will show the JSON content of the original document. - If not yet restored, a "Restore" button will be visible.
- The
- Clicking "Restore":
- The system attempts to re-create the document from the stored JSON data.
- If successful, the "Deleted Document" record is updated (
restored = 1,new_nameis set), and the button on the form changes to "Open".
- The "Deleted Document" records themselves can be periodically purged using the
clear_old_logsfunction (typically via a scheduled job) to manage database size.
This Doctype acts as an important safety net, allowing recovery from accidental deletions and providing an audit trail of what was deleted.