DEV Community

Cover image for how to write client script for delete doctype all data in frappe
Anil
Anil

Posted on

how to write client script for delete doctype all data in frappe

Here's a blog post that incorporates the script you provided:


Deleting WhatsApp Messages in Batches Using Frappe

Managing large amounts of data in any system can be challenging, especially when it comes to deleting records efficiently. In Frappe, we can handle this scenario by customizing the list view and deleting records in batches to avoid overloading the server.

In this post, we’ll explore how to set up batch deletion of "WhatsApp Message" documents using custom code within Frappe.

Why Batch Deletion?

When dealing with thousands or more records, trying to delete them all at once can strain your server, causing timeouts or slow performance. Batch processing allows us to break the task into smaller, manageable chunks, reducing the load on the server and ensuring a smoother user experience.

Customizing the List View

First, we'll modify the list view of the "WhatsApp Message" doctype to add a button for batch deletion. This button will trigger the process of deleting all WhatsApp messages in batches.

Here’s the code to customize the list view:

frappe.listview_settings['WhatsApp Message'] = {
    onload: function(listview) {
        // Add a custom button in the list view
        // Uncomment this section to enable the button
         listview.page.add_inner_button(__('Delete All Docs'), function() {
            // Confirm before proceeding with the deletion
             frappe.confirm(
                 'Are you sure you want to delete all Whatsapp messages?',
                 function() {
                     Start the batch deletion process
                     delete_in_batches();
                 }
             );
         });
    }
};
Enter fullscreen mode Exit fullscreen mode

The commented-out part of the code adds a button labeled "Delete All Docs" to the list view. Once clicked, it prompts the user for confirmation before starting the deletion process. You can uncomment this part of the code to enable the button in your environment.

The Batch Deletion Function

The core of this process is the delete_in_batches function. This function fetches a batch of documents, deletes them one by one, and then proceeds to the next batch with a delay, ensuring the server is not overwhelmed.

Here’s the full code for batch deletion:

function delete_in_batches(batch_size = 2000) {
    // Step 1: Fetch a batch of documents to be deleted
    frappe.call({
        method: "frappe.client.get_list",
        args: {
            doctype: "WhatsApp Message",  // Your target doctype
            fields: ["name"],  // Fetch only the 'name' field (docnames)
            limit: batch_size  // Process in batches of 2000
        },
        callback: function(response) {
            let docs = response.message;

            if (docs && docs.length > 0) {
                // Step 2: Loop through each document in the batch and delete it
                docs.forEach(function(doc, index) {
                    frappe.call({
                        method: "frappe.client.delete",
                        args: {
                            doctype: "WhatsApp Message",  // Ensure this is correct
                            name: doc.name
                        },
                        callback: function(r) {
                            if (!r.exc) {
                                frappe.show_alert({
                                    message: `Document ${doc.name} deleted`,
                                    indicator: 'green'
                                });
                            }
                        }
                    });
                });

                // Step 3: Delay the next batch to prevent server overload
                setTimeout(function() {
                    delete_in_batches(batch_size);  // Recursive call to delete the next batch
                }, 2000);  // 2-second delay between batches
            } else {
                // No more documents to delete
                frappe.msgprint("All WhatsApp messages have been deleted.");
                frappe.listview.refresh();  // Refresh the list view once the process completes
            }
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

How It Works:

  1. Fetching Documents:

    The function starts by fetching a batch of WhatsApp Message documents, with the size of the batch controlled by the batch_size parameter (default is 2000).

  2. Deleting Documents:

    For each document fetched, the script calls the frappe.client.delete method to delete the document. After each successful deletion, a notification is shown to the user.

  3. Recursive Batching:

    Once a batch is processed, the function pauses for 2 seconds before fetching and deleting the next batch. This delay helps prevent server overload.

  4. Completion:

    When there are no more documents to delete, a message is displayed to the user, and the list view is refreshed to reflect the changes.

Conclusion:

This script is a simple yet effective solution for batch-deleting large amounts of data in Frappe. By breaking the deletion process into smaller, manageable batches, we avoid overloading the server and ensure that the operation runs smoothly. You can modify the batch size and delay to suit your needs, depending on the volume of data and server capacity.

Feel free to integrate this script into your own Frappe application to streamline the deletion of WhatsApp messages or any other document type.

After write client script write this logic in server script select in sever script type API and write this code in script

Here’s a detailed explanation of the code with the code itself included:

import frappe

@frappe.whitelist()
def delete_all_docs(doctype):
    # Get all documents of the specified doctype
    docs = frappe.get_all(doctype)

    # Loop through each document in the list
    for doc in docs:
        # Delete the document using its name and the provided doctype
        frappe.delete_doc(doctype, doc.name, force=1)

    # Commit the changes to the database to make deletions permanent
    frappe.db.commit()

    # Return a confirmation message
    return f"All documents from {doctype} have been deleted."
Enter fullscreen mode Exit fullscreen mode

Code Breakdown:

  1. import frappe: This imports the frappe module, which is the core of the Frappe framework, allowing you to access database and server functionalities.

  2. @frappe.whitelist(): This decorator allows the function delete_all_docs to be accessible via Frappe’s API and callable from external applications or scripts.

  3. def delete_all_docs(doctype):: The function is defined to accept one parameter:

    • doctype: The type of document (e.g., "Employee", "Customer") from which all records will be deleted.
  4. docs = frappe.get_all(doctype): This fetches all documents of the specified doctype and stores them in the docs variable. Each document returned will include the document name, which is used for deletion.

  5. for doc in docs:: The function loops through each document in the list docs.

  6. frappe.delete_doc(doctype, doc.name, force=1): Inside the loop, each document is deleted by specifying the doctype and the doc.name. The force=1 argument ensures that the document is forcefully deleted, bypassing validation rules and checks if necessary.

  7. frappe.db.commit(): Once all documents are deleted, the database changes are committed, making the deletions permanent.

  8. return f"All documents from {doctype} have been deleted.": After all documents are deleted, a success message is returned, indicating the operation was successful.

This function is useful when you need to delete all documents from a particular doctype in a Frappe application.

Top comments (0)