What I have done before that works well for me is to first pry the box away from the stud using a screwdriver. You only have to create a small gap between the box and stud. Then I use a sawzall and cut the nails holding the box to the stud. Next carefully pull the box and wire out of the wall. I usually don't have too much trouble doing it this way.