Server Script to delete Document from other doctype if removed from table

Hi,
We created a custom doctype (A) that has a child table (C-A) and added a server script for it to create a new record on another doctye (B). How ever we would like to have another function to delete from doctype(B) if we remove a row from child table (C-A)

Also, When I try to update the document it will not proceed and throw an error that says “doctye (B)” “docname” already exist

here is the code for we used to create the entry in the doctype (B)

for i in doc.coordinator:
frappe.get_doc(dict(
    doctype = 'Principal 2022',
    full_name = i.full_name,
    first_name= i.first_name,
    middle_name = i.middle_name,
    last_name = i.last_name,
    docstatus = 0,
    lead_coordinator = doc.name,
    cluster = i.cluster,
    )).insert()
frappe.msgprint(msg=_(f'Teacher Profile has been created'), title=_('Automation'),indicator='green')

for i in doc.coordinator:
frappe.get_doc(dict(
    doctype = 'Campus 2022',
    full_name = i.full_name,
    first_name= i.first_name,
    middle_name = i.middle_name,
    last_name = i.last_name,
    docstatus = 0,
    lead_coordinator = doc.name,
    )).insert()
frappe.msgprint(msg=_(f'Campus Profile has been created'), title=_('Automation'),indicator='green')