These days I am working on water station where several stages will clean up the water first tank from mud and inject chem to kill bacteria and other bio organism, before pumping the clean water up to the final tank.
To solve the above problem, I first shut off the pump, drain the contaminated water, clean and decontaminate the tanks and then turn on the pump again.
This is a life support process and can't leave it for equations and calculations. Residue and impurities can remain for many reasons if not cleaned.