Plant-specialized secondary metabolites have ecological functions in mediating interactions between plants and their entophytes. Here, we aimed to reveal the interaction between flavonoid synthesis and endophytic bacterial communities in wild Ginkgo trees spanning 100-800 years. We found that flavonoids including quercetin, kaempferol and isorhamnetin decreased while the microbial diversity in leaves increased with the age of sampled trees. Older trees had more unique genera and shifted their endophytic bacterial community structure. Also, Mantel tests and correlation analysis indicated a generally significant (p < 0.05) negative correlation between endophytic bacterial communities and flavonoids. Additionally, both deterministic and stochastic processes could play roles in the assembly of endophytic bacterial communities in Ginkgo trees with a progressive increase in stochastic processes as flavonoid concentrations decreased. This study provides a mechanistic understanding of how flavonoids modulate the endophytic microbial community assembly.