I hang my head in shame at finding worms in one of my tanks.

As most of you know, I'm in the process of rearranging my tanks. Yesterday I moved the fish & shrimps from my river tank to a spare tank, so I could re-work the river tank set up. After draining the tank as far as possible I started to remove the last of the rocks & pebbles, and stirring up the sand substrate. It was after stopping for a coffee that I noticed the worms on the glass.
A bit of rummaging around the internet provided me with a lot of information about various worms that could be found in a freshwater aquarium. Planaria seemed to be the most common worm that would be found attached to the glass, though the pictures I found of that indicated a pointy head that could be seen with the naked eye.
https://www.practicalfishkeeping.co.uk/features/articles/how-to-solve-a-problem-like-planariansThere is another flatworm that looks similar, but without the pointy head, so I could have those. Or I could have planaria that are quite young, and have yet to develop the pointy head.

The pictures that I have managed to get using my phone also show the worms sort of scrunched up, rather than stretched out like planaria. They are more long/thin when moving, but I'm struggling to get a picture of that because they are so tiny.
I know I had a lot of algae eaters in the river tank, and seemed to constantly be pooh hoovering, but these worms show that the tank was much dirtier than I realised. Although I feel very embarrassed and ashamed about this, I feel that I have to share this on the forum, not only as part of the ups and downs of fishkeeping, but also to go through resolving the issue and reducing the chances of the problem reappearing in the future.
On the bright side, and I'm struggling with bright sides in this situation, I hadn't seen these worms while the tank was running, so the worms weren't so prolific that they had taken over the tank. Also, as I have stripped down the tank to rework it, it means I can treat the problem without worrying about the fish & shrimps.
My first response to seeing these worms was

followed by

then

. I had an urge to buy a flame-thrower and burn the entire tank. Ok, so a tad extreme, so now I'm just taking all the sand out and cleaning it with tap water. I'll get some Genchem No-Planaria ordered as well, and will be able to over-dose with this as the sand is not in the tank.
The initial information that I found regarding the worms that look similar to planaria (can't find it now, but will look again later today) was that they would not be killed by the planaria treatment. So I plan to treat the sand with marine salt (which I have for my brackish mudskipper tanks), just in case. After this I am also tempted to treat with boiling water. In my mind this is the closest I can get to the initial flame-thrower response, and I also hope that between the No-Planaria, marine salt, and boiling water, I should also deal with any eggs in the sand.
If anyone else has ever encountered any similar issues and has any advice, please let me know.
I already have plans for re-working the river tank, including additional filters, which should help to ensure that this problem does not re-occur. I will be making some other changes that should help, and will go through those when I'm setting up the tank again. Basically, all the information I found pretty much goes along the lines of "pick up your cleaning & tank maintenance regime you skanky moo" (obviously I'm paraphrasing).
I'm off to hang my head in shame whilst rinsing the sand.
