First, if it’s a severe experience, you might seek a counselor of some sort.
I occasionally have minor issues along those same lines. My experience with such things is that it is very hard to purposefully refrain from thinking something. (For me, some stupid thought flits in. Then I say “I must not think X,” but of course that very phrase contains X, which then repeats itself over and over.) There are only two ways I’ve ever had even limited success with:
complete inner silence. Try to shut down the inner voice. It’s hard, and occasionally the stupid thought will flicker.
try to drown it out with repetitive mental prayer. This sometimes works, but unless I really, really, really focus on the prayer (and sometimes even then) the stupid thought I’m trying to suppress will repeat in the background.
Of course, I’m saying the bad thought “will repeat”, and “repeats itself” as though it were it’s own creature and not something that I’m doing, which is false. But certainly isn’t intentional - more reflexive for some reason.
Mainly though, know that as these things are not intentional, they are not sins. Here is an excerpt from the 10 commandments for the scrupulous that I think you might find helpful - it doesn’t directly address what you say, but it does touch on the same issue(there are two different versions of these, both of which are linked below).
- You shall not hesitate to look at any crucifix or at any statue in church or at home or anywhere else because you may get bad thoughts in your mind and imagination. If such thoughts occur, they carry no sin whatever.
Although this commandment deals with a situation that is not necessarily a problem for all scrupulous persons, it is nevertheless a real burden for some. If you try to avoid the problem by not looking, the problem will tend to become more severe. It is a much better choice to meet the problem head on. Thoughts and imaginations that occur in this situation are simply not sinful. One should try and confront fear, not give in to it.