Abstract Finding a suitable oviposition site is a challenging task for a gravid female fly, since the hatched maggots have limited mobility, making it difficult to find an alternative host. The oriental fruit fly, Bactrocera dorsalis , oviposits on many types of fruits. Maggots hatching in a fruit that is already occupied by conspecific worms will face food competition. Here, we showed that maggot-occupied fruits deter B. dorsalis oviposition and that this deterrence is based on the increased β-caryophyllene concentration in fruits. Using a combination of bacterial identification, volatile content quantification, and behavioural analyses, we demonstrated that the egg-surface bacteria of B. dorsalis , including Providencia sp . and Klebsiella sp ., are responsible for this increase in the β-caryophyllene contents of host fruits. Our research shows a type of tritrophic interaction between microorganisms, insects, and insect hosts, which will provide considerable insight into the evolution of insect behavioural responses to volatile compounds.