-- https://blog.devart.com/reduce-msdb-big-size.html
DECLARE @DateBefore DATETIME
SET @DateBefore = DATEADD(DAY, -30, GETDATE())
EXEC msdb.dbo.sysmail_delete_mailitems_sp @sent_before = @DateBefore --, @sent_status = 'sent'
EXEC msdb.dbo.sysmail_delete_log_sp @logged_before = @DateBefore