Background: The gut microflora of insects plays important roles throughout their lives. Different foods and geographic locations change gut bacterial communities. The invasive wood-borer Agrilus mali causes extensive mortality of wild apple, Malus sieversii, which is considered a progenitor of all cultivated apples, in Tianshan forests. Recent analysis showed that the gut microbiota of larvae collected from Tianshan forests showed rich bacterial diversity but the absence of fungal species. In this study, we explored the antagonistic ability of gut bacteria to address this absence of fungi in the larval gut. Results: The results demonstrated that gut bacteria were able to selectively inhibit wild apple tree-associated fungi. However, Pseudomonas synxantha showed strong antagonistic ability, producing antifungal compounds. Using different analytical methods, such as column chromatography, mass spectrometry, HPLC and NMR, an antifungal compound, phenazine-1-carboxylic acid (PCA), was identified. Activity of the compound was determined by the minimum inhibitory concentration method and electron microscopy. Moreover, sequence alignment and cluster analysis showed that gut bacteria could originate from noninfested apple microflora during infestation. Conclusions: Overall, the results showed that in newly invaded locations, A. mali larvae changed their gut microbiota and adopted new gut bacteria that prevented fungal colonization in the gut.