Delete all records in specific master data

Hello,

Please suggest me that how can I delete all records in specific master data for example all items in Stock? Now, there are over thousands items in my stock, and I’m deleting by selecting all items that showed about 100-150 records / time.

Thanks

Regards,

Hii
This can delete all invoices for you if they do not have any link issues.

  • goto frappe-bench/
  • bench console
for invoice in frappe.get_list('Sales Invoice'):
    invoice = frappe.get_doc('Sales Invoice', invoice.name)
    invoice.cancel()
    invoice.delete()

Refer this: https://frappe.github.io/frappe/current/api/model/frappe.model.document

Hope it helps
Thank you :slight_smile:

2 Likes

Thanks so much for your reply. I’m not sure if it works for items in Stock because they are master data not a transactional document.

Simply remove cancel method, since that is for transactions only.
Everything else should be the same. Do know that this wont work if you have any other transaction created based on the item you are deleting.

Thanks so much for your reply. Could you please guide me more, how can I get all items using frappe.get_doc? and if there are multiple sites, how can I specify a stock that I’d like to remove all items in each site?

Regards,

frappe.get_doc gives a document. thats why we are using a loop.
Actually i dont quite understand what you meant by ‘specifying a stock that you would like to remove’.

Which master would you like to get cleared ?

bench --site [sitename] console

Using that, we can remove items from different sites.

According to your post, if I change something like below, is it right?

for item in frappe.get_list(‘Stock’):
item = frappe.get_doc(‘Stock’, item.name)
item.delete()

There is no Stock doctype in ERPNext.
If you want to delete all the items, use this:

for item in frappe.get_list('Item'):
    item = frappe.get_doc('Item', item.name)
    item.delete()
1 Like

Hi @cipher,
If you want to delete everything without any worry of linkage to another doctype you can use the following commands:

bench --site <sitename> mysql
delete from `tab<doctype-name>`;

Please note that it will delete everything from the database table of that doctype and it may cause problem if there are other documents linked with the deleted records.

1 Like

Hello,
we have an issue of not getting upgrade from version 10 to 11 forward due to a custom app causing issues. We should delete the occurrences of “PageMaster” in doctypes and defaultvalue,
Would this idea given here work for that? Or should we go for sql select statements etc? (not so familiar with those SQL statements, so your solution looks better.)

There is also then extra tables after that we won’t need:
tabPageMaster Blog Post
tabPageMaster Page
tabPageMaster Page Item

Also, “bench update” fails because it can’t find the Module “PageMaster” but then on the other hand if you go to bench console, it says this:
In [1]: frappe.get_installed_apps()
Out[1]: [‘frappe’, ‘erpnext’, ‘pagemaster’]

How should I remove it totally since app remove commands won’t help, neither uninstall?

If we delete the whole rows e.g. from “tabDocType”, it supposed it would make damage?

INSERT INTO tabDocType VALUES (‘About Us’,‘2018-03-14 18:02:33.190162’,‘2018-04-12 20:17:04.213476’,‘Administrator’,‘Administrator’,0,NULL,NULL,NULL,0,NULL,1,0,1,1,‘PageMaster’,NULL,NULL,NULL,‘’,NULL,NULL,NULL,‘modified’,‘DESC’,NULL,NULL,0,0,NULL,NULL,NULL,0,0,0,0,0,0,0,NULL,0,‘’,NULL,NULL,NULL,NULL,‘InnoDB’,NULL,0,0,NULL,0,0,0,0,0,NULL,NULL,NULL,NULL,NULL,1),

The version update is not going through until this custom app is totally removed.
At the moment there is only “Internal Server Error” visible on the IP address of the ERPNext installation.

For some reason, none of the solutions worked for me.

Accessing the ‘frappe.db’ object only worked:

frappe.db.delete(‘Stock’)

To delete all records in a specific doctype using the console:

bench --site {sitename} console
import frappe
frappe.db.sql("DELETE FROM `tabdoctype name`")
frappe.db.commit()