Discard something valuable along with something not wanted.
The earliest known reference to the phrase is in Thomas Murner’s Narrenbeschwörung (Appeal to Fools), which is dated 1512.