Wipe HTC One M8 for sale without losing root
What I think you are looking for is this: http://www.htc.com/us/support/htc-one-m8/howto/696022.html

However I am not sure if this removes the root. If it does, there is also another possibility, depending if you have this option. If you already flashed rom's, you may have done it trough fastboot. In the fastboot menu, there should also be an option to delete data (or wipe data) and delete cache. That will delete or your stored files on the phone, without loosing root.