If enemies attacked with only ranged, they also leave.
Which is because the infantry inside the wall outnumber the enemy infantry outside the wall and deem it worth the risk, to the chagrin of their commander I'm sure.
And Zakky/Koitalike family raises a good point on non-militia leaving walls if they're set to aggressive or murderous.