I was actually quite astounded when I read this as this is exactly the behavior I had when I was around his age. In fact, the wording is exactly the same. I used to have an uncontrollable compulsion to "confess" every single bad thing or bad thought that entered into my head over the course of a day, and I too referred to them as "bad thoughts." They were extremely upsetting at the time as I felt like I could not control the thoughts popping into my head. My thoughts also ran along the lines of violent ideas (which I would never act upon), "sexual things", and so on.
Looking back on it now, I do believe that the thoughts themselves are not the issue. All children can have "disturbing" thoughts (though, from an adult perspective, a lot of these thoughts aren't terribly disturbing. They can be upsetting to kids as they do not understand.) It's possible that your little boy is more upset by the feeling that he is unable to control the thoughts or stop them from happening. I don't believe it's important to stop the thoughts from happening, but it is important to stop the resultant ruminating over them because, of course, the mere act of worrying over a thought will keep it around. Sorta like that thing where someone challenges you to not think of a pink elephant, no matter what you do.
It's possible, too, that your boy doesn't understand that these thoughts are normal in people of all ages. Everyone can have violent, disturbing or strange thoughts once in a while. I know for me, I continually needed to 'confess my sins', as it were, because I was afraid that by thinking these things, there was the possibility I would do them. Confession also gave me a sense of relief. Oftentimes, OCD behavior is constrained to a single thing and other behavior can be entirely normal. I think that this behavior has less to do with wanting to be a "good boy," though that can certainly be part of it. When I think about how I felt at the time, the intense guilt came more from not confessing something rather than actually doing something... I never, ever had the desire to actually act upon the thoughts I had. The idea that you cannot control your thoughts can be very frightening. I do agree with Marc... it does sound akin to OCD behavior. In my case, I still have similar behavior as an adult and have both mild OCD and anxiety.
I am really glad that you're doing all that you can to help your sweet little boy. When I look back on how I felt with my "bad thoughts," it breaks my heart. My mother also has experience with mental illness (not to say your boy has a mental illness, of course) and so she was very understanding. I should also mention that I am an only child and had a pretty picturesque childhood, so I do not think it is a question of bad parenting at all. Do you have any other children?
I am going to keep thinking about this because it makes me so sad to hear that your boy is having a tough time. I'll try to remember what finally solved it for me, if that would help.