During 2007–2009 and 2012–2014, avian influenza virus (AIV) was studied in a wild avian community of a northern Spanish wetland using non-invasive sampling methods and host identification by COI barcoding. The aim of this longitudinal study was to evaluate AIV dynamics in a natural wetland ecosystem, taking into account both virological aspects and ecological traits of hosts. Global AIV prevalence decreased significantly during the second sampling period (0.3%) compared to the first (6.6%). Circulating subtype distributions were also different between periods, with a noteworthy H5 and H7 subtype richness during the first sampling period. Mallard Anas platyrhynchos was identified as the main AIV host, although not all positive samples could be ascribed to the host. We modelled AIV prevalence with regard to the avian host community composition and meteorological data from the wetland. Statistical analysis revealed seasonal differences in AIV detection, with higher prevalence during the breeding season compared to other phenological events. The model also shows that the lower AIV prevalence during the second study period was associated with a significant reduction of breeding Anseriformes in the wetland, revealing a long-term fluctuation of AIV prevalence driven by the breeding Anseriformes community. This longitudinal study on AIV epidemiology in a natural ecosystem reveals that although prevalence follows seasonal and annual patterns, long-term prevalence fluctuation is linked to the breeding community composition and size. These results are relevant to understanding the influence of host ecology on pathogen transmission for preventing and managing influenza emergence.Electronic supplementary materialThe online version of this article (10.1186/s13567-019-0623-5) contains supplementary material, which is available to authorized users.