Is there a way to delete a message if it's not successfully handled in a given time period?

A few ways to do this.

  • If you are modeling a business process, it is best to model them via a Saga. The ‘discard’ needs to be mapped to a business reason and it can be implemented as a policy. For example (off top of my head), in a saga that manages user’s account activation, if a user does not activate the account after an email is sent, the process removes the whole process via a TimeOut. Read more about saga timeouts here.

  • You can use ‘Time To Be Received’ (TTBR) attribute (or by applying it via a convention) on your message which essentially says after a certain time elapses it does not make sense to process that message anymore and the system will discard the message. It won’t happen. You need to be aware that you might lose messages if your only endpoint is down for some time and the messages get expired. Read more about this here. Also, in MSMQ, when the message expires it is NOT moved to DLQ (to avoid disk consumption) so it can not be recovered.

Hope this helps.