It's not quite clear from the post -- Does he see these boys outside school AND in school as well? You mention that you don't want him sitting alone in his room, which I take to mean that he spends time with this group outside school. You can handle that one swiftly and firmly: You control his time when he's not in school. He needs extracurricular activities that interest him and where he will meet other kids who are going to share his interests. If there aren't clubs at school that interest him, go through the class catalog for your local parks and recreation department, or wherever there are supervised, organized teen activities for his age. If he likes computer games, get him into a computer game programming course for teens (these do exist). For certain non-computer games now popular with some teens, there are playing sessions at libraries in our area. You will need to be very proactive and will need to involve him in making choices he really likes and wants, but he truly needs to be occupied. Homework, activity, back home, more homework or family time--that's plenty for a middle school kid. And getting into some solid activities with adults who are good role models, and other kids who are interested in the activity and don't want to goof around but want to be DOING the activity, is positive.
If he is connecting with this group of boys on social media, do you monitor his accounts? You should. Be sure you know what accounts he has and all passwords, and beware -- he could have hidden accounts you don't know about, frankly. If you've never before been clear with him that you have the power to monitor his phone (if he has one), tell him that now. Don't do a big dance about how "this is so you won't communicate with those boys" but just start checking his calls, texts, Instagram if he's there, etc., daily, and in a very matter-of-fact way.
In school it's harder for you to control, but I would meet with the teacher who gave him detention. Go alone, without your son. Talk with the teacher about what he or she is seeing and give the teacher your full backing for this detention and any other steps the teacher needs to take. Don't give the impression that you want the detention undone--I'd support it instead. I would ask the teachers who have these boys and your son in classes together, to ensure that your son not be seated near these kids (many classes change seats every quarter anyway so that's a natural break for your son to be moved without anyone thinking it's weird). Ask that he never be allowed to do group projects with them--some teachers assign groups, and if that's the case, be clear that he should not be assigned to work with these boys, and if the teacher says "I let kids choose their own groups," I'd still say, "I'm asking that he not work with these kids."
In our MS there was one vice principal for each grade and those vice principals tended to be the administrators, other than counselors, who really knew the kids in that grade pretty well. I know some parents said the vice principal was the one to put on alert that a kid was having issues or that kids were being a problem. If your school has someone like that, use that resource.
You might want to see the school counselor yourself to ask about this kind of situation and get ideas for what to say to your son and other tactics for persuading him to separate from this group. The counselors know this age group really well and will have insights for parents!
One caution. A parent banning a friend makes that friend into forbidden fruit -- and as the saying goes, forbidden fruit is the sweetest. These boys might be more attractive as friends if you rail against them to him, or insist that they're changing him for the worse (even though it's true). Be cool and collected, don't badmouth these boys in detail, but be clear that this detention really got your attention. Then move on and get him into activities outside school. And stay in touch with teachers inside school.
As for the worry that "he will have few, if any friends" -- like B says below, no friends are better than bad ones. Activities can help him find new friends, or at the very least, keep him from hanging out with bad ones.