How to Keep Flies out of an Indoor Pet Cage
To keep flies out of an indoor pet cage, clean the cage as often as possible by removing any uneaten food, waste, and soiled bedding. You should also replace the water in the cage every day since stale, stagnant water can attract flies. Also, try setting up a fan near your pet's cage to improve the ventilation, which can help reduce bad smells that flies are attracted to. If the problem persists, consider placing a fly screen over the cage so flies can't get in.
Clean the cage as often as possible. Depending on the type of pet you have, and the type of food you feed your pet, you may need to clean the cage as frequently as once a week, or do a partial cleaning after feeding. For instance, if your pet eats fruit, remove any scraps or uneaten fruit as soon as they are finished eating. Flies, as well as other types of unwelcome critters, will be drawn to warm, wet parts of a cage in particular. Be especially wary of any organic matter in the cage that might decompose, especially bits of food and plant material. Use a natural cleaner in either spray or wipe form to fully clean every surface in the tank. Replace substrate and bedding frequently. Your substrate may include mulch, soil, shredded paper, or a number of other materials that will readily soak up moisture and eventually begin to decompose. Whatever the substrate is, it needs to be replaced frequently. The specific type of substrate you use and pet you have will dictate the specific frequency with which the substrate needs to be changed, but stay on top of a consistent schedule. When replacing the substrate, remove everything from the cage and give it a deep cleaning with a safe, non-toxic cleaner. Take care the wipe down the corners and crevices at the bottom of the tank that bits of damp substrate may have nestled into. Whenever you notice spoiled bedding or flooring, replace it. Get in the practice of replacing these materials before waste or moisture becomes prevalent. Use a water bottle instead of a dish. Dishes will allow for greater moisture to splash out and collect in the substrate and elsewhere in the tank, as well as a much greater likelihood that the water itself becomes an attraction to flies. If the type of pet you have allows for the use of a water bottle, do so. Of course, some caged pets will require bowls to lounge in and otherwise enjoy. Get a size-specific water-bottle for almost any furry cage pet from the pet store. These will most often be suspended on the side of your tank, to be easily access by both you and your pet. Change any water in the tank daily. Whatever type of water feature or dispenser you use, be sure to change the water every day. Stale, stagnant water, can rapidly become an insect breeding ground, especially in warm environments.  With reptile tanks, which will likely be generally wetter and warmer than mammal tanks, be particularly conscious of the cleanliness of both the substrate and any mini bodies of water. Use fly screens. Even if you have a screen-topped tank or a cage that allows small insects in and out, fly netting will still allow adequate ventilation while cutting down on flies' ability to access the treasure trove that is your pet's tank. Screens of this sort are especially important to consider when raising a rabbit in a cage of any type, as fly infestations can be fatal to rabbits. Ventilate the cage properly. The buildup of ammonia from your pet's waste will readily attract flies. Regular cleaning of the tank and replacement of the tank's substrate will help you prevent the buildup of ammonia, but adequate ventilation is vital, as this will allow the ammonia that is inevitably present to be largely removed by circulating air. Keep the air in the room where you keep your pets moving with a ceiling or box fan. Keep your pet’s food away from their cage. Especially if your pet eats fruit or veggies – or mammals, for that matter – keep these foods and the materials your use to prepare them away from your pet's cage. Keep fruit in your kitchen, where you'll be able to keep it fresh longer, and dispose of any fruit that you don't refrigerate as soon as it shows signs of age. Be careful to remove and wash dishes with which you feed your pet a short time after feeding. This is somewhat dependent on the type of food you pet eats, but fruit in particular calls for cleaning later that same day. Keep doors and windows that don’t have screens closed. Flies are out there, lurking in the wild just beyond the walls of your home, and waiting for a chance to enter. Stay diligent about keeping access points closed from disallowing the entrance of flies into you and pet's domain. Alternatively, if you prefer to have windows and doors open, keep your pet in a room that will always be sealed from the exterior, at least via entirely undamaged screens. Regulate the temperature of the room. In general, the cooler and drier you keep an indoor pet cage, the fewer flies you'll get. If it's especially warm in the room where you keep your pet, consider keeping the pet in a different room or doing what you can to cool the room where they live. Don’t keep garbage in the same room. It's a well-known fact that flies are attracted to garbage.  Keep garbage receptacles in different rooms, to prevent flies that are attracted by garbage from occupying your pet's tank as well. Relatedly, dispose of fruit in particular by either bringing it outside or putting it in a garbage disposal. Make a funnel trap. For larger flies, such as house flies, paper funnel traps are surprisingly effective. To make one, use stiff paper to make a funnel that will sit within the rim of a jar you will use as your trap. Put a ½ an inch of water with a tablespoon or two of sugar into the bottle of the jar. Cut the tip of the paper cone so there's a hole at the bottom of the paper funnel about one inch in diameter and place it in the jar, making sure that the tip of the funnel is not submerged or nearly submerged in the sugar water. Flies will enter the funnel, but will be unable to easily leave. Dispose of them as you wish, while leaning towards the more permanent options. Make a fruit fly trap. Put about 1" of apple cider vinegar or balsamic vinegar in the bottom of a glass or jar. Add a couple of drops of liquid dish detergent and swirl to dissolve. Place glass just outside the pet cage overnight. Dispose of the liquid and captured flies and replace the trap if live flies are still present. Remove the trap once there are no longer flies in the cage Similarly, you can simply put a ½ inch of beer or red wine in a bottle with a tall neck. A wine bottle is actually ideal. Flies will enter the bottle but struggle to leave, perhaps in part because they'll be drunk. Use commercial fly traps. If the infestation persists, you can try using commercially available traps, many of which are also natural and chemical-free. Some even use color to help attract flies, and many simply use glue. Replace these traps as they fill up, and discontinue use only once they are no longer acquiring victims. 
Clean the cage as often as possible. Replace substrate and bedding frequently. Use a water bottle instead of a dish. Change any water in the tank daily. Use fly screens. Ventilate the cage properly. Keep your pet’s food away from their cage. Keep doors and windows that don’t have screens closed. Regulate the temperature of the room. Don’t keep garbage in the same room. Make a funnel trap. Make a fruit fly trap. Use commercial fly traps. 