is it possible to undo a transaction as long as it does not have any confirmations?
if yes, how?
You can't "undo" but you can create a competing transaction and try to get that mined first.
Most nodes will not relay a double spend so you'ld need to somehow get the transaction sent directly to a miner who would include that double spend instead of the original transaction.
But even then it becomes a race. If the transaction has a good chance of being mined in the next couple of blocks your miner partner would need tens of TH/s of mining capacity to have a 50% chance or more of being able to mine the double spend. That gets very expensive to do. :-)