Find the source where the drain flies spawn and seal it air tight with a water lock. In my case it was where the washer drain entered the water lock pipe: it was put in too deep so when it flushed the water lock was emptied and the flies could escape that way.

Only top voted, non community-wiki answers of a minimum length are eligible