Entomophagy has emerged as an alternative and viable source of high-quality animal-derived proteins and fats for livestock production or as a human food supplement. The use of black soldier fly larvae (BSFL) as a dietary protein source for animals needs to be hygienically safe. The authors analysed the microbial community structure of the BSFL raised on different diets and investigated the effect of various heat treatments on the bacterial composition through 16S rRNA gene amplicon pyrosequencing. The results revealed that the diet of BSFL could alter the bacterial community structure. However, there are substantial differences in the abundance or diversity of individual bacterial populations. The bacterial community was dominated by four common phyla, including Proteobacteria, Firmicutes, Bacteroidetes, and Actinobacteria. Most of the samples show the presence of some potential foodborne pathogens such as Bacillus, Aeromonas, Campylobacter, Pseudomonas, Proteus, Klebsiella, Helicobacter, and Enterococcus. Furthermore, looking at the microbial community structure of BSFL, the post-harvest heat treatment showed a significant effect on human pathogens when compared to those in the control group without heat treatment. Enterococcus in BSFL raised on soybean curd residues (SCR) diet disappeared after heat treatments in all groups. Moreover, Staphylococcus in BSFL raised on artificial feed (AF) diet disappeared after heat treatment. The heat-treated samples of BSFL reared on SCR, and AF (Group OD22 samples dried in the oven for 22 minutes and at a temperature of 150 °C) had a better influence on the community structure as compared to all heat treatments. This study provides the baseline data on the microbial composition and potential pathogens of BSFL reared on different dietary sources and showed the effect of different heat treatments on the microbial community.