SQL Server – Purging Database Mail History
SQL Server stores all mails and attachments in msdb database. To avoid unnecessary growth of msdb database you should remove these mail history unless it is required for auditing or other purposes.
To check all mails processed by Database Mail, you can use sysmail_allitems catalog view:
If you are frequently sending larger attachments using database mail this can cause msdb to grow rapidly. All attachments stored in msdb database are available in sysmail_attachments.
To delete mail items you can use system stored procedure sysmail_delete_mailitems_sp, it has below syntax:
sysmail_delete_mailitems_sp [@sent_before] [@sent_status]
You can delete mail using either of the parameters, @sent_before deletes all mail that were sent before specified date, and @sent_status deletes all mails with specified status.
For example, to delete all mails which are sent and are older than current month we can use:
@sent_before = '2012-05-10 00:00:00',
@sent_status = 'sent'
(100 row(s) affected)
You can query the sysmail_event_log view to check the deletions that has been initiated.
ORDER BY log_date DESC
Mail items deletion is initiated by user “sa”. 100 items deleted.
DatabaseMail process is started
Hope This Helps!