error on script deleting record of related table

error on script deleting record of related table

I have two forms:
Purchase which logs company purchases,
and Cashflow which shows the cash balance of the company.

when creating a purchase,if a decision box called "add to cash flow" is  checked, a new Cashflow record is created with the relevant details. 
Purchase also has a lookup table to the newly created cashflow record.

when the decision box is unchecked, or the purchase record is deleted, i want the associated cashflow record to be deleted.

I know a bi-directional lookup might assist but it can't be because every purchase record might also have a "tax return cashflow record" so i can't maintain a one to one relation.

I get this error on submission:

image.png

here is the code at the onValidate workflow that deletes the record
cat = Category_Cashflow[Name == "תשלום לספקים"];
taxCat = Category_Cashflow[Name == "החזר מעמ"];
cf = CashFlow[Purchase == input.ID && Category == cat]; //Cashflow Record
tax_cf = CashFlow[Purchase == input.ID && Category == taxCat]; //Tax return Cashflow record

...

// line 119
if(input.Add_To_Cash_Flow == false)
{
if(cf != null && cf.count(ID) > 0)
{
input.CashFlow_Tax_Refund = null;
CashFlow_Link = null;
delete from CashFlow[ID == cf.ID];
}
if(tax_cf != null && tax_cf.count(ID) > 0)
{
CashFlow_Link = null;
// delete from CashFlow[ID == tax_cf.ID];
}
}

thank you for helping