Populations of ecological systems generally have demographic fluctuations due to birth and death processes. At the same time, they are exposed to changing environments. We studied populations composed of two phenotypes of bacteria and analyzed the impact that both types of fluctuations have on the mean time to extinction of the entire population if extinction is the final fate. Our results are based on Gillespie simulations and on the WKB approach applied to classical stochastic systems, here in certain limiting cases. As a function of the frequency of environmental changes, we observe a non-monotonic dependence of the mean time to extinction. Its dependencies on other system parameters are also explored. This allows the control of the mean time to extinction to be as large or as small as possible, depending on whether extinction should be avoided or is desired from the perspective of bacteria or the perspective of hosts to which the bacteria are deleterious.