This really sounds like an mental health issue to me... not a religious one. So, from that perspective, my thinking would be this is something that needs to be worked on with the help of your psychiatrist & therapist if you have these mental health professionals in your life. Imagining there is something you could do by yourself to stop these thoughts may well just be making you feel worse because it causes you to beat yourself up for not being able to figure out how to do it. I'm sorry you are struggling with these difficult thoughts.