Fungus gnats feed on damp, decomposing organic matter that sits on the surface. To prevent them from reaching their food source, cover kitchen scraps with soil every time they are added to the compost pile or wrap them in one layer of newspaper and bury deep in the compost pile.
Fungus gnats like a most environment. To reduce the chances of attracting them, keep an eye on the moisture level of the compost pile. It needs to be moist but not wet to work properly and prevent insect problems.
Fungus gnats hate coffee grounds, but fruit flies love them. Place coffee grounds on top of the soil at least once a week to discourage gnats.
Beneficial nematodes feed on fungus gnat larvae. To use this control measure, add beneficial nematodes to soil but allow several weeks for this approach to work.