How to compact OSM xml files?

I have a few local files that I use for building map overlays for my Garmin. I just noticed that when I edit files in JOSM it just marks objects as deleted rather than purging them from the file so the file size never decreases. Is there a utility for purging deleted nodes and ways from a .osm file?

JOSM has a “purge” action in that’s separate from deletion:
To get rid of the elements you’ve already deleted, give these instructions a try:

Hope it works as intended! Alternatively, I guess you might also be able to “clean up” your file by selecting everything (Ctrl + A), copying (Ctrl + C), creating a new layer (Ctrl + N) and using the “Paste at source position” command (Ctrl + Alt + V) in the new layer. Then save that new layer to file. Double-check if all relations and other complicated elements have survived the procedure, first. (And be sure to have a backup, I’ve never tried this myself. ;))

Purge seems to just be a hard delete for stuff that is still visible in JOSM. Doesn’t have any effect for previously deleted items.

I ended up doing a copy / paste since it was a fairly small area, reducing file from 14 MB to 695 K.

If you use mkgmap to process the file it should not matter because mkgmap should ignore data that is not visible.