Calcium and magnesium form undesirable salts and soaps with carboxylic acids in crude oil, acting as emulsifiers and causing sedimentation and blockages. Furthermore, NaCl and MgCl2 produce corrosive HCl. The presence of vanadium and nickel, even in trace amounts, poisons catalysts and necessitates their reduction, especially since they concentrate in heavy residues. Solvent extraction is employed to remove these heavy metals, thus desalting and purifying the crude oil.