There are several instructions from the Buddha that might help here, from specific approaches (e.g. "changing focus of attention", recollections, etc) to broad practices (like meditation and loving-kindness).
“Here, bhikkhus, when a bhikkhu is giving attention to some sign, and owing to that sign there arise in him evil unwholesome thoughts connected with desire, with hate, and with delusion, then he should give attention to some other sign connected with what is wholesome. When he gives attention to some other sign connected with what is wholesome, then any evil unwholesome thoughts connected with desire, with hate, and with delusion are abandoned in him and subside. With the abandoning of them his mind becomes steadied internally, quieted, brought to singleness, and concentrated. [...]
“If, while he is giving attention to some other sign connected with what is wholesome, there still arise in him evil unwholesome thoughts connected with desire, with hate, and with delusion, then he should examine the danger in those thoughts thus: ‘These thoughts are unwholesome, they are reprehensible, they result in suffering.’ When he examines the danger in those thoughts, then any evil unwholesome thoughts connected with desire, with hate, and with delusion are abandoned in him and subside. With the abandoning of them his mind becomes steadied internally, quieted, brought to singleness, and concentrated. [...]
“If, while he is examining the danger in those thoughts, there still arise in him evil unwholesome thoughts connected with desire, with hate, and with delusion, then he should try to forget those thoughts and should not give attention to them. When he tries to forget those thoughts and does not give attention to them, then any evil unwholesome thoughts connected with desire, with hate, and with delusion are abandoned in him and subside. With the abandoning of them his mind becomes steadied internally [...]
“If, while he is trying to forget those thoughts and is not giving attention to them, there still arise in him evil unwholesome thoughts connected with desire, with hate, and with delusion, then he should give attention to stilling the thought-formation of those thoughts. When he gives attention to stilling the thought-formation of those thoughts, then any evil unwholesome thoughts connected with desire, with hate, and with delusion are abandoned in him and subside. With the abandoning of them his mind becomes steadied internally, quieted, brought to singleness, and concentrated. Just as a man walking fast might consider: ‘Why am I walking fast? What if I walk slowly?’ and he would walk slowly; then he might consider: ‘Why am I walking slowly? What if I stand?’ and he would stand; then he might consider: ‘Why am I standing? What if I sit?’ and he would sit; then he might consider: ‘Why am I sitting? What if I lie down?’ and he would lie down. By doing so he would substitute for each grosser posture one that was subtler. So too … when a bhikkhu gives attention to stilling the thought-formation of those thoughts … his mind becomes steadied internally, quieted, brought to singleness, and concentrated.
“If, while he is giving attention to stilling the thought-formation of those thoughts, there still arise in him evil unwholesome thoughts connected with desire, with hate, and with delusion, then, with his teeth clenched and his tongue pressed against the roof of his mouth, he should beat down, constrain, and crush mind with mind. When, with his teeth clenched and his tongue pressed against the roof of his mouth, he beats down, constrains, and crushes mind with mind, then any evil unwholesome thoughts connected with desire, with hate, and with delusion are abandoned in him and subside. With the abandoning of them his mind becomes steadied internally, quieted, brought to singleness, and concentrated. Just as a strong man might seize a weaker man by the head or shoulders and beat him down, constrain him, and crush him, so too … when, with his teeth clenched and his tongue pressed against the roof of his mouth, a bhikkhu beats down, constrains, and crushes mind with mind … his mind becomes steadied internally, quieted, brought to singleness, and concentrated.