Remove Unsent Database Email from SQL Server
Estimated study time: 6 minutes. Clean up a stuck Database Mail queue safely.
SQL Server's Database Mail feature queues emails before sending them, and sometimes messages get stuck in an Unsent or Retrying state — usually because of an SMTP misconfiguration or a mail server that's unreachable. Here's how to inspect and clear that queue.
Checking Mail Status
Start by looking at the status of items sitting in the mail queue:
SELECT mailitem_id, subject, sent_status, send_request_date FROM msdb.dbo.sysmail_allitems WHERE sent_status = 'unsent';
Stopping the Mail Queue
Before deleting anything, it's safest to stop the Database Mail external program so nothing tries to send while you clean up:
EXEC msdb.dbo.sysmail_stop_sp;
Deleting Unsent Mail Items
You can remove specific unsent items using their mailitem_id, or clear all of them in one go:
DELETE FROM msdb.dbo.sysmail_mailitems WHERE sent_status = 'unsent';
sysmail_log entries first, or use sysmail_delete_mailitems_sp instead of a raw DELETE.Using the Built-in Cleanup Procedure
SQL Server also ships a safer procedure for this exact scenario:
EXEC msdb.dbo.sysmail_delete_mailitems_sp @sent_status = 'unsent';
Restarting the Mail Queue
Once the queue is clean and the SMTP configuration is fixed, restart Database Mail:
EXEC msdb.dbo.sysmail_start_sp;