Getting 100% CPU when emails and communications are deleted

I’ve been getting 100% cpu on both cores (4Gb mem VPS) whenever I delete emails or communications from ERPNext.
This lasts for about two minutes per email so something isnt right. I think thsi started happening about a month ago, as I dont recall this issue previously.

Specifically the issue is this:-

When the emails are deleted, Background jobs shows the below for the time the mysql process is very busy.

frappe.model.delete_doc.delete_dynamic_links

So why is it taking so long checking links ? Is this process working correctly?
The server is specced fine and normally has very low loading so I’m certain its related to ERPNext itself.

I really want to get this rolled out to my users, but when this issue occurs the site gets really sticky and slow to respond not surprisingly.

Where can I go to debug the mysql further ? And has anyone else seen this occur? I can’t test this on the demo.erpnext.com server

I’m running up to date Frappe /ERPNext on Master on Ubuntu 1604 LTS with the easy install script install method

ERPNext: v8.0.29 (master)
Frappe Framework: v8.0.47 (master)

In my case one email delete takes a second or so - I am on ERPNext: v8.0.24 (master)
Frappe Framework: v8.0.42 (master) with just a few hundred emails.

Is each email a DocType?

Delete touches on communications for eg.

https://github.com/frappe/frappe/commit/ebbcd4593e3cb7fac06f2b3ad9c05fc3f4cf88dc

This might identify bottleneck - to refine a query or maybe add an index?

Hi @clarkej

I’ll look at the commit you mention -n the timing was about then that my problems started. But I dont know why others dont seem to see this. Getting 100% cpu kinder screws things up, so I dont know if its just an issue I’m seeing or something else

I’m not really a DBA, so I can probably only make simpler adjustments.

That said, the deleting is quick but if you delete say 10 emails, the background Jobs fills with the delte dynamic links process which takes about two minutes just for one email. I wonder if I have a permissions issue with communications as this wasn;t right before where I needed to add ‘Amend’ and ‘Delete’ privs to email users to allow them to delete emails.

This all really should work out of the box as my set-up is pretty vanilla, no custom scripts or odd setups.

I want to get the Frappe help for this ie paid help but just waiting on this at the mo.

After deleting 20 emails, its still running mysql at 100% now, 20 mins after I deleted the emails.