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';
💡 Tip: If deletes are blocked by foreign key constraints on related mail log tables, clean up 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;

Finding Out Why Mail Got Stuck

Clearing the queue without fixing the cause means it will fill up again. Before or after cleanup, spend a few minutes on diagnosis. Start with the Database Mail event log, which records errors from the external mail program:

SELECT TOP (20) log_id, event_type, log_date, description
FROM msdb.dbo.sysmail_event_log
ORDER BY log_date DESC;

Messages here usually point straight at the problem, such as authentication failures, a wrong SMTP port or a server that refused the connection. Then check the configuration and the state of the mail system:

EXEC msdb.dbo.sysmail_help_status_sp;               -- STARTED or STOPPED
EXEC msdb.dbo.sysmail_help_queue_sp @queue_type = 'mail';
EXEC msdb.dbo.sysmail_help_account_sp;              -- SMTP server, port, SSL setting
EXEC msdb.dbo.sysmail_help_profile_sp;

SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb';

Database Mail depends on Service Broker in msdb. If is_broker_enabled returns 0, messages are queued but never processed, which looks exactly like a stuck queue.

Common Causes and Fixes

Symptom in the logLikely causeWhat to check
Authentication failedWrong or expired SMTP credentialsUpdate the account with sysmail_update_account_sp
Could not connect to serverWrong host or port, or a firewall ruleConfirm the SMTP host, port and SSL option with your mail admin
Mail stays unsent with no errorsDatabase Mail stopped or broker disabledStatus procedure and is_broker_enabled
Some mails failed, others sentInvalid recipient addressessysmail_faileditems view for the affected items

After fixing the account, send a test message to confirm the path works end to end:

EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'AlertsProfile',
     @recipients   = 'dba-team@example.com',
     @subject      = 'Database Mail test',
     @body         = 'If you can read this, Database Mail is working.';

Keeping the Mail Tables Small Over Time

Every message and log entry stays in msdb until removed, so busy servers can build up large history tables. A scheduled SQL Server Agent job that purges old items keeps msdb healthy:

DECLARE @cutoff DATETIME = DATEADD(DAY, -30, GETDATE());

EXEC msdb.dbo.sysmail_delete_mailitems_sp @sent_before = @cutoff;
EXEC msdb.dbo.sysmail_delete_log_sp      @logged_before = @cutoff;

Choose a retention period that matches your audit needs; the value in the example is only a placeholder.

Safe Practices for This Task

  • Prefer the system procedures over raw DELETE statements on msdb tables, since the procedures handle related rows correctly.
  • Run cleanup with an account that has the right permissions, typically a sysadmin; ordinary members of DatabaseMailUserRole can only manage their own items.
  • Save a list of the unsent subjects and recipients before deleting, in case important alerts need to be re-sent.
  • Add monitoring on the queue length so a growing backlog raises an alert early.

Finally, document the SMTP details and the cleanup job in your runbook. Mail problems tend to appear when someone changes a password or a relay server, often months after Database Mail was set up, and a short written checklist turns a confusing outage into a five-minute fix for whoever is on call.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with Uncodemy's Data Analytics Course.

Explore Data Analytics Course →