The fisrt step is to install custom javascript plugin on your wordpress website and copy the below code.

Then you need to hover over Appearance option in wordpress and paste the code.

setTimeout(() => {
 window.history.replaceState({}, "", window.location.href.split("#")[0]);
}, 100);

This is 100% working code for How to Remove anchor from url on scroll WordPress using jquery and javascript.