You can only cancel a pending transaction if it has not yet been added to a block. This can be confirmed by copying and pasting the transaction ID into its respective block explorer here:
If the transaction has at least 1 confirmation in the block explorer, it has already been added to a block and cannot be cancelled or expedited. OR If the transaction cannot be found on the block explorer, then it has not been broadcasted. These are considered "stalled" transactions, which can be removed with these instructions.
The process for cancelling or speeding up are very similar; however, there may be subtle differences depending on which wallet you are using. It essentially requires sending a brand new transaction, but with the same nonce as the pending one.
Here are instructions for several of most common types of wallets:
If you have sent a long queue of multiple transactions, you may want to cancel or speed up the very first of them to avoid congestion. Once the first one completes or is cancelled, the rest will be able to finish the confirmation process.