Try ERPNext Buy Support Partners Foundation

How can I Cancel & Delete 100,000+ Sales Invoices?

Hello Mates,
I just imported 100,000+ Sales Invoices and realized that I have made a mistake in data. Can you help me delete all records in this DocType?

I have tried the following bench console command method but seems it is not working:

for invoice in frappe.get_list(‘Sales Invoice’):
invoice = frappe.get_doc(‘Sales Invoice’, invoice.name)
invoice.cancel()
invoice.delete()

Thanks in advance for your help!

Perhaps try something like:

frappe.db.delete('Sales Invoice', {
    'creation': '<datetime>'
})

https://frappeframework.com/docs/user/en/api/database

1 Like

Thanks for your help. How can I cancel the Document before deleting?

Maybe use the frappe.db.set_value(doctype, name, fieldname, value) method to change the value in Sales Invoice status to Cancelled. My use of the api has been limited. Make a back up before proceeding or clone the existing instance and use the duplicate to test with if possible.

1 Like

How did you import so many? the data import tool takes a long time and sometimes times out at around 1000 invoices…

Hi @satishvijayan, I have used Bench CLI to import large amount of data. Our 600,000+ journals took 24+ hours to import. I have also used tmux to run the process without keeping the session on.

You can also import using:
bench --site sitename data-import --doctype Item --file /path/to/file.csv

1 Like

Finally I did it using Bench Console

for invoice in frappe.get_list(‘Sales Invoice’):
invoice = frappe.get_doc(‘Sales Invoice’, invoice.name)
invoice.cancel()
invoice.delete()
frappe.db.commit()

Using tmux it took nearly 8 hours to complete the process.

1 Like