I understand that some operations cost negative gas because they free up space on the blockchain, such as selfdestructs. What are all the operations that can have negative gas? Does this only occur because the operation ultimately frees up space on the blockchain or are there other reasons it has negative gas?
The only two OPCODEs with negative gas costs are STORAGEKILL(-15000) and GSUICIDEREFUND(-24000).
These occur when storage values are deleted or contacts are suicided.
These OPCODEs grant gas refunds because they free up space in the blockchain.
The prices of all OPCODEs can be found here
As a practical compliment to the Tjaden's answer, in Solidity a gas refund is issued when
delete on existing data is made:
delete aassigns the initial value for the type to
a. I.e. for integers it is equivalent to
a= 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements reset. For structs, it assigns a struct with all members reset.
deletehas no effect on whole mappings (as the keys of mappings may be arbitrary and are generally unknown). So if you delete a struct, it will reset all members that are not mappings and also recurse into the members unless they are mappings. However, individual keys and what they map to can be deleted.
It is important to note that
delete areally behaves like an assignment to
a, i.e. it stores a new object in