As the old saying goes, an ounce of prevention is worth more than a pound of cure. With that in mind, the best practice is to stay ahead of the game. However, sometimes conditions and circumstances are such that major fly hatches will occur. For example, we've had a particularly wet Spring here in Georgia, and moisture plus chicken poop equals FLIES! (How's that for chicken math?!)
When my best efforts fall short, and when I have a huge fly hatch, I mix a 5-gram scoop of Bayer Tempo Ultra WP into a 1-gallon pump sprayer and spray for a quick knockdown because I don't want adult flies hanging around, laying eggs, and compounding the problem. On the good side, Tempo is relatively safe to use around chickens and inside coops and pens, and it is biodegradable and quickly degrades. For example, I always mix it fresh when I use it because it loses half its strength within a few days. In other words, it's safe to use indoors and outdoors. On the downside, Tempo will take out some beneficial insects including honeybees, and it can harm fish if used around a pond or fish tank. If you do decide to use this product, please READ THE DIRECTIONS first before using. It's a great product for me, but it may not be the best for you.
A longer-term preventative is to use garden lime (NOT hydrated lime) in the coops, runs and bedding areas. Just about any feed store will carry garden lime, and it's very inexpensive (For example, a 50# sack runs about $3). Fly larvae need an acidic soil or medium in order to thrive. Garden lime will change the pH of the medium to more alkali on the pH scale, and thus, a less hospitable environment for fly larvae to develop. BTW, garden lime is harmless to chickens, and many of your plants will love it.