One reason houseflies enter the home is that they detect decaying organic material, such as overripe fruit or spoiled meat. Houseflies are also drawn by moist trash from an uncovered garbage bin.
Houseflies need moist organic matter to feed on and to provide a spot to lay their eggs. They are usually found on feces and rotting meat, as well as any uncovered food. They can eat a variety of foods including human food, excrement and carrion.
Uncovered food draws flies. Throw out fruit when it starts to rot, and pick up any rotten fruit that falls off fruit trees in the yard. Drain moisture from food before throwing it out. Do not leave dirty plates sitting out for long. Scrape the food waste into a bag, tie the bag, and store it in a garbage can with a tightly sealed lid.
Compost piles and trashcans outside the home are breeding grounds for flies. To avoid drawing flies, close trashcans tightly, making sure the lid fully shuts. Ensure that the trash bins have no holes, and put trash out for collection regularly. Periodically scrub garbage cans to prevent build-up that might attract flies. Add some sawdust to the compost pile to cut down on moisture and lessen the chances of drawing flies.