Bacteria have ways to specifically delete unwanted genes. They have methylases that "label" their own DNA with methyl groups. And they have restrictases that will destroy any DNA that does not have these "labels".
We have mechanisms that edit our genomic DNA in a very precise and specific way. This is used in immune system, where random fragments of DNA are snipped out to create a diverse set of new genes. Here's an example:
Say your genome is ABCabcdefg12345. In immune cells this mechanism would cut out a single capital letter, single lower-case letter and a single number and put them together. Thus different cells will have different final genes:
Aa2
Cf5
Be5
Ag1
etc.
These genes code for antibodies and therefore each cell has a different antibody gene, which can recognize a different pathogen.
Most viruses that we encounter don't destroy our DNA and they don't add to our DNA. They replicate as separate fragments of DNA or RNA. Only some viruses integrate their genome into ours. Most famous of those are retroviruses and our genomes are full of them. But majority of viruses don't do that.