Hey, Anna! Sounds to me like you have OCD–the cycle of “upsetting intrusive thought > anxiety > perform action to lessen anxiety” is there (it’s not limited to needing cleanliness). I also have difficulty with masturbation and interrupting intrusive thoughts, and I realize it’s not always as easy as “just letting go” of them. My sympathies are with you. It really sucks. There are all kinds intrusive thoughts, whether they’re about religious purity or relationships or violence against others or even worse! But the good news is, everyone has them–some brains (like ours) just assign significance to them and get really anxious.
This has been a great resource for me: http://drmartinseif.com/dms/intrusive-thoughts/
And if you need more help, I would strongly suggest seeing a therapist who specializes in Cognitive Behavioral Therapy or Exposure-Response Therapy for stuff like this. Best of luck!