The trick to “cancel” your pending transaction is by replacing the transaction with another 0 ETH transaction but higher gas fee sending to yourself with the same nonce as the pending transaction.
Warning! You must use same nonce, otherwise your new push tx will consider as new tx in ethereum network.
Theorically you just have below than 20 seconds interval to carry out this action.
To cancel ERC20 type transaction, you need to set higher gas fee and regenerate data (hex) with 0 token amount.
Tips! You can also cancel by sending normal ETH transaction (gas limit 21000), provided total gas fee = gas limit * gas price is higher than the previous tx.