For bmesh code I often used tag to flag a visited element. On noticing the tags persisted it was required to "detag" the entire bmesh element collection on each run, for consistent results.
Have used a workaround of tagging via a default dictionary as demoed here https://blender.stackexchange.com/a/199075/15543
Below is a little test script to run in edit mode. Randomly tags and selects. Does not have a call to update the edit bmesh so the selection does not show in the edit window. Run enough times and all edges are tagged.
If the object is toggled back into edit mode the selection persists, all tags are reset to default of False
import bpy
import bmesh
from random import choice
context = bpy.context
ob = context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)
print("Selected", len([e for e in bm.edges if e.select]))
print("Tagged", len([e for e in bm.edges if e.tag]))
for e in bm.edges:
e.tag = e.tag or choice((True, False))
e.select_set(e.tag)
Is there a way to quickly discard any selection made or tag set without iterating and resetting all elements?