That's not going to work.

Flies have a complete metamorphosis life cycle, so if the existing flies have laid eggs in the water again, you'll have more of them.

In order to keep them away, you'll need to do a complete water change in the enclosure. Another idea would be to stock the water portion with some small fish (white cloud minnows or zebra danios are both good, hardy choices) if there is room, because they will just eat any fly larvae. But the fish idea would only work if you have 3-5 gallons of water or so, and >5 gallons would be better. Otherwise, you just need to keep the water cleaner so that the larvae don't have anything to eat. Most filters have cartridges that stop absorbing nitrogenous wastes (NH3, NO2-, NO3+, etc.) after about a month, depending on the bioload in the tank. If you replace your filter when you are supposed to and do more frequent (partial) water changes, the flies should vanish.

It should also be noted, however, that the flies could also persist in potted plants in the house (in the water tray underneath the plant, etc.) or other standing water left out in the house. If you want them gone for good, you might need to make sure that there aren't any other sources of water like that available to them.