If you cancel a unit, or cancel research for tech or for an upgrade, you get back its full cost in minerals and gas. Some bots know how to cancel a zerg egg if it is about to die, to save the cost. For example, Steamhammer cancels eggs, lurker eggs, and guardian/devourer cocoons that are very low on hit points (not the ideal criterion, but it helps).
If you cancel a building that is under construction, you get back 75% of the original cost in gas and minerals, rounded down to the nearest integer. Buildings tend to be expensive, so many bots cancel buildings that are about to be destroyed. If a terran SCV is killed while constructing a building, the player should either dispatch another SCV to finish the building, or cancel the building, depending on the situation; it’s not an easy decision. If the building is destroyed before it is completed, which is easier because it doesn’t have all its hit points yet, you lose the full value.
But these are not all the cases!
If a building is carrying out research when it is destroyed, the research is cancelled by force majeure. You get back 75% of the research cost, again rounded down. Do any bots cancel research when a tech building is about to be destroyed? Steamhammer does not (yet). You save 25% of the research cost, which is not much, and you accept a risk that the building might survive (maybe the enemy will stop shooting it), and you’ll have canceled for nothing.
If a building is producing units when it is destroyed, you are refunded the full cost of the units. It’s the same whether the unit is actually in production, or is queued behind other units for later. There is still a theoretical advantage to canceling pending units that will not be produced, though. It returns the resources sooner (minerals, gas, and supply), which might be valuable if you are resource constrained and can use the resources another way.
The refund for pending units of a destroyed building is an advantage for terran and protoss (though not a big one). Zerg has to worry about canceling eggs and cocoons, and must accept the risk of canceling incorrectly versus losing the unit for nothing.