Is it possible to cancel a Bitcoin transaction ...
... that has not yet been transferred?
Yes, obviously this is possible. I suggest you select this method in your Bitcoin application, showing the user a confirmation screen for several seconds with transaction information and the cancel and confirm buttons. @nahtnam suggested a 60-minute delay, which is too long, given that you may lose Internet access or want the transaction to appear in the blockchain as soon as possible.
... which already was ?
Maybe ... To cancel such a transaction, you need to create a block yourself, which moves the inputs of the original transaction to one of your own addresses. (Actually canceling the original transaction.) However, creating a block costs several thousand dollars at the moment, and finding a block is not guaranteed.
Another possibility is to broadcast another transaction with the same inputs as the original, and target the outputs to one of your own addresses / wallets. To encourage miners to include this transaction instead of the original, you increase the transaction fee. However, some clients cannot transfer such a double spent transaction to the miners, and some miners may reject the double spent transaction and include the original (the one they received at the beginning), instead. (cf Bitpay encountered zero double costs in the first 10,000 transactions and Cancel an unconfirmed tranasaction on @theymos (bitcoin.stackexchange) )
There are several tricks to hide your initial transaction from the miners, creating the so-called "non-standard" transaction. Alternatively, the transaction may include a very low fee for the miners to reject it for economic reasons. (cf Significant losses due to bilateral unconfirmed transactions (bitcoin-dev mailing list) and Double spending on @petertodd (reddit) ) However, this makes your transaction suspicious to the recipient if they look at it, and they will probably ask you to wait until she has one or more confirmations.
Finally, it is also possible if you set a flag in the original transaction to indicate a replacement. You can then replace the original transaction with another by including the same (one or more) inputs in the replacement transaction. In addition, you have to pay a higher fee. However, not all miners comply with this flag, and some may still include your initial transaction. (cf BIP 125 reference : Enabling full replacement on behalf of )
... which is included in one or more blocks?
No, hardly impossible. You will need to manage a significant amount of hashing in order to create a fork by restoring the block chain, starting from the block before the initial transaction occurred and ended on the block with height = (current public block chain height) + 1 . Therefore, "the more evidence you have, the more difficult, expensive and unreliable such an attack is." Source: @DannyHamilton (Bitcointalk) Satoshi Nakamoto Bitcoin paper explains that this is always possible when you control more than 50% of the hash power and possibly with a probability of less than 1, but more than 0, if you control the hash power of less than 50%. See Bitcoin: Peer-to-Peer Electronic Money System . Although, if you control a significant amount of hashing power, you are likely to be encouraged not to undermine the confidence in Bitcoin by eliminating transactions and indirectly damaging your flow of mining income.