Overview
Hangup handlers are subroutines attached to a channel that will execute when that channel hangs up. Unlike the traditional h extension, hangup handlers follow the channel. Thus hangup handlers are always run when a channel is hung up, regardless of where in the dialplan a channel is executing.
Multiple hangup handlers can be attached to a single channel. If multiple hangup handlers are attached to a channel, the hangup handlers will be executed in the order of most recently added first.
Full documentation page here:
Hangup Handlers (Asterisk Project)
Note: Hangup handlers were introduced in Asterisk 11, and therefore many long time Asterisk users may not be aware of this new feature. If you have always wanted your Asterisk server to perform some action at the time a call is hung up, a hangup handler might be the solution you have been looking for.