It sounds like he has a medical problem. Punishments aren't the answer. Take him to a doctor.
Since he has had this all his life and since he is on medication for the problem, it is possible that you can not make it stop! Trying to make it stop could be like trying to stop a childhood disease. Of course, you really haven't said what med he is on or what the doctor has explained to you - so I may be barking up the wrong tree. I do hope you have asked your doctor plenty of questions.
To answer your question - and this is assuming that what he has is a medical condition. What you can do is to make his life non stressful. You don't spank him or make him clean his underwear! You do make sure that his school knows what is going on. You keep a pair of clean underwear for him in the school office. You make sure his teacher knows the situation and that he will need to go to the bathroom and quite possibly at non standard times. Also make sure the school principal knows what is going on. With the very little info you have given us - this is the best I can do. I can guarantee that this will at least make his life a little better.
He has had this all his life and been on med for over 3 months
Does this happen all the time or only at school?
What meds did the doctor give you? if it was a fiber medication, it can take a good week or more for it to make a difference. How long did you try it.
Finally, how long has this been going on? Just this year (which means something very different than if he has had this problem all his life)
My mother in law made one of her grandsons clean out every poppy underwear he had. Maybe try that?
http://www.alyson.ca/2008/03/still-pooping-p.html
Here is a link that you might find helpful.
Other than giving him the spanking of a lifetime, I can't think of anything else to suggest...and I am not normally one to suggest spanking a child. I thought if you made your home like a prison, with no privileges whatsoever, it would get his attention, but it sounds like you have done that already. I am stumped. If I can think of anything else that sounds like a good idea, I will post it here for you.
As I said we have tried everything
Have you tried grounding him or taking away privileges, or both? Maybe not allowing him to do anything except sit in his room with no friends, no phone, no tv or video games, no going out to McDonald's with the family...just take away all privileges, whatever they may be. Or, maybe you could make him do a chore that is really tedious and boring If the doctor has determined that there is nothing medically wrong with him, if you ground him or take away privileges, or give him a boring or unpleasant chore to do around the house, and be consistent with the punishment, maybe he will stop making messes in his pants and start using the toilet.