refactor: use delete_contact_and_address (#34497)

Co-authored-by: Deepesh Garg <deepeshgarg6@gmail.com>
diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py
index 2a588d8..a98886c 100644
--- a/erpnext/crm/doctype/lead/lead.py
+++ b/erpnext/crm/doctype/lead/lead.py
@@ -3,7 +3,10 @@
 
 import frappe
 from frappe import _
-from frappe.contacts.address_and_contact import load_address_and_contact
+from frappe.contacts.address_and_contact import (
+	delete_contact_and_address,
+	load_address_and_contact,
+)
 from frappe.email.inbox import link_communication_to_document
 from frappe.model.mapper import get_mapped_doc
 from frappe.utils import comma_and, get_link_to_form, has_gravatar, validate_email_address
@@ -40,9 +43,8 @@
 		self.update_prospect()
 
 	def on_trash(self):
-		frappe.db.sql("""update `tabIssue` set lead='' where lead=%s""", self.name)
-
-		self.unlink_dynamic_links()
+		frappe.db.set_value("Issue", {"lead": self.name}, "lead", None)
+		delete_contact_and_address(self.doctype, self.name)
 		self.remove_link_from_prospect()
 
 	def set_full_name(self):
@@ -119,27 +121,6 @@
 			)
 			lead_row.db_update()
 
-	def unlink_dynamic_links(self):
-		links = frappe.get_all(
-			"Dynamic Link",
-			filters={"link_doctype": self.doctype, "link_name": self.name},
-			fields=["parent", "parenttype"],
-		)
-
-		for link in links:
-			linked_doc = frappe.get_doc(link["parenttype"], link["parent"])
-
-			if len(linked_doc.get("links")) == 1:
-				linked_doc.delete(ignore_permissions=True)
-			else:
-				to_remove = None
-				for d in linked_doc.get("links"):
-					if d.link_doctype == self.doctype and d.link_name == self.name:
-						to_remove = d
-				if to_remove:
-					linked_doc.remove(to_remove)
-					linked_doc.save(ignore_permissions=True)
-
 	def remove_link_from_prospect(self):
 		prospects = self.get_linked_prospects()
 
diff --git a/erpnext/crm/doctype/prospect/prospect.py b/erpnext/crm/doctype/prospect/prospect.py
index fbb1158..8b66a83 100644
--- a/erpnext/crm/doctype/prospect/prospect.py
+++ b/erpnext/crm/doctype/prospect/prospect.py
@@ -2,7 +2,10 @@
 # For license information, please see license.txt
 
 import frappe
-from frappe.contacts.address_and_contact import load_address_and_contact
+from frappe.contacts.address_and_contact import (
+	delete_contact_and_address,
+	load_address_and_contact,
+)
 from frappe.model.mapper import get_mapped_doc
 
 from erpnext.crm.utils import CRMNote, copy_comments, link_communications, link_open_events
@@ -16,7 +19,7 @@
 		self.link_with_lead_contact_and_address()
 
 	def on_trash(self):
-		self.unlink_dynamic_links()
+		delete_contact_and_address(self.doctype, self.name)
 
 	def after_insert(self):
 		carry_forward_communication_and_comments = frappe.db.get_single_value(
@@ -54,27 +57,6 @@
 					linked_doc.append("links", {"link_doctype": self.doctype, "link_name": self.name})
 					linked_doc.save(ignore_permissions=True)
 
-	def unlink_dynamic_links(self):
-		links = frappe.get_all(
-			"Dynamic Link",
-			filters={"link_doctype": self.doctype, "link_name": self.name},
-			fields=["parent", "parenttype"],
-		)
-
-		for link in links:
-			linked_doc = frappe.get_doc(link["parenttype"], link["parent"])
-
-			if len(linked_doc.get("links")) == 1:
-				linked_doc.delete(ignore_permissions=True)
-			else:
-				to_remove = None
-				for d in linked_doc.get("links"):
-					if d.link_doctype == self.doctype and d.link_name == self.name:
-						to_remove = d
-				if to_remove:
-					linked_doc.remove(to_remove)
-					linked_doc.save(ignore_permissions=True)
-
 
 @frappe.whitelist()
 def make_customer(source_name, target_doc=None):