Delete Events from the Database Mail Log in SQL Server (T-SQL)

When you use Database Mail to send emails in SQL Server, you can use the sysmail_delete_log_sp stored procedure to delete items from the event log.

You can choose to delete all events, events based on their logged date (i.e. before a certain date) or events based on the event’s type.

View All Events

Before you start deleting any events, you might want to take a look at them. You can do this with the sysmail_event_log view.

SELECT * FROM msdb.dbo.sysmail_event_log;

Delete Old Events

To delete all events that were logged before a certain date, use the @logged_before argument.

Example:

EXECUTE msdb.dbo.sysmail_delete_log_sp  
    @logged_before = '2020-08-25';

Delete Events of a Certain Type

You can use the @event_type argument to delete only events of a given type.

Here’s an example of deleting all informational events.

EXECUTE msdb.dbo.sysmail_delete_log_sp  
    @event_type = 'information';

Delete All Events

To delete all events, execute sysmail_delete_log_sp without any arguments.

EXECUTE msdb.dbo.sysmail_delete_log_sp;

Deleting Mail Items

The sysmail_delete_log_sp procedure is solely for deleting entries from the log. It does not delete the mail items from the Database Mail tables.

Use sysmail_delete_mailitems_sp to delete email items from the Database Mail tables.