Horizontal gene transfer (HGT) is known to promote the spread of genes in bacterial communities, which is of primary importance to human health when these genes provide resistance to antibiotics. Among the main HGT mechanisms, natural transformation stands out as being widespread and encoded by the bacterial core genome. From an evolutionary perspective, transformation is often viewed as a mean to generate genetic diversity and mixing within bacterial populations. However, another recent paradigm proposes that its main evolutionary function would be to cure bacterial genomes from their parasitic mobile genetic elements (MGEs). Here, we propose to combine these two seemingly opposing points of view because MGEs, although costly for bacterial cells, can carry functions that are point-in-time beneficial to bacteria under stressful conditions (e.g. antibiotic resistance genes under antibiotic exposure). Using computational modeling, we show that, in stochastic environments (unpredictable stress exposure), an intermediate transformation rate maximizes bacterial fitness by allowing the reversible integration of MGEs carrying resistance genes but costly for the replication of host cells. By ensuring such reversible genetic diversification (acquisition then removal of MGEs), transformation would be a key mechanism for stabilizing the bacterial genome in the long term, which would explain its striking conservation.