This is so awful for you, Op. You haven't failed him; you are doing your best.
It sounds as if there is something else going on with him. My first thought was that he is struggling academically, therefore giving out the 'I don't care' message and the swearing in class. But from what you have been told, his academic performance is good.
However, that doesn't mean that he feels successful or capable at what he is being asked to do. The reality and what his own mind tells him may be miles apart.
I just had a young teen stay with me (fostering) who had a similar attitude. The school wanted to exclude her because of repeated bad behaviour, refusing to attend class, being disruptive, and walking out. On the days she walked out, she was also getting the bus into town and going AWOL for hours.
It seemed the teachers were being very hostile towards her, too.
After some very long chats at my home, which she instigated when we sat together doing some craft, it came out that she felt she was incapable in maths. However, she was passing tests, albeit that she felt she was finding it an increasing struggle to do so. This was leading her to give the "f this" attitude in class, walking out many times. She was also beginning to self-harm. It's a kind of self-protection to say she didn't give a s about school just because she was sure she would begin failing tests and exams.
A bit like when we say, 'I don't love him anyway' when someone is about to dump us in a relationship!
It has emerged as a confidence issue, coming from something else that's going on in her life. She's back on track now, after lots of confidence boosting and the promise of extra maths help if she needs it. So far, she is managing now that she is getting extra mental health support and now she knows she doesn't have to struggle on alone in maths.
I wonder if there is something else troubling your boy, leading to the 'there's no point' stance.
It is hard to say whether the school have been unfair to your son or not; I'd say it often comes down to safeguarding.
If they feel his behaviour puts him or someone else at risk, then they did the only thing they could. It depends on the exact circumstances. But in your position, I'd focus on what you can do now to boost his self-esteem as that seems to be low.
Eilonwy's experience, posted above, is similar to those I have come across and this could be the case for your son too. There's a reason for him school refusing, but it may be related or unrelated to school if that makes sense.