Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up
Remove Unsent Database Email from SQL Server

Remove Unsent Database Email from SQL Server

Intermediate 3 min read 18 Sep 2025 167K views
Remove Unsent Database Email from SQL Server

How to Remove Unsent Database Email from SQL Server?

Suppose, you are sending mail to different users by using a while loop and you forgot to insert a while loop update statement. In this case, SQL Server will generate thousands or millions of emails against a specific email address within a minute.

85% of data analyst roles require SQL Server proficiency. Start your journey with our Free SQL Server Courses Online now!

Read More: SQL Server Interview Questions and Answers

Read More - Top 50 SQL Interview Questions And Answers For Freshers

To stop the SQL Server from sending unwanted mail we are required to clean the unsent mail from the database mail queue. We can do this by running the below queries.


SELECT * FROM msdb.dbo.sysmail_event_log;
-- To get number of unsent emails
select count(*) from msdb.dbo.sysmail_unsentitems;
-- remove all the unsent emails
delete from msdb.dbo.sysmail_unsentitems; 

Read More: Basics of SQL Commands

Read More - DBMS Interview Questions For Freshers

Explanation of the Code

  1. Retrieving Unsents Email Messages:
    
    SELECT * FROM msdb.dbo.sysmail_event_log;
    

    This query retrieves information about email events from the sysmail_event_log table. It can be useful for monitoring the status of email operations.

    
    select count(*) from msdb.dbo.sysmail_unsentitems;
    

    This query calculates the number of unsent email messages in the sysmail_unsentitems table.

  2. Removing Unsents Email Messages:
    
    delete from msdb.dbo.sysmail_unsentitems;
    

    This statement deletes all unsent email messages from the sysmail_unsentitems table, effectively clearing the email queue.

    All the unexpected emails have been removed from the SQL Server database mail queue.

Summary

In this article I tried to explain how you can remove unsent emails from the database mail queue. If you want to gain a practical understanding, you can enroll in our SQL Server Course.

Do you Know?

.NET is gaining popularity day by day, especially after the release of .NET 8. .NET 8 is not only a framework version but much more than that. It redefines the way software applications are built and deployed, enabling developers to meet the evolving demands of modern computing.

80% of full-stack roles now require .NET expertise. Don’t miss out—join our full stack .NET developer course and future-proof your career!

Test yourself in 5 minutes

Take the free Sqlserver skill challenge to see your strengths and the gaps to fix next.

Start the challenge
Share LinkedIn X Facebook WhatsApp

About the author

Shailendra Chauhan

Shailendra Chauhan

Microsoft MVP, Founder & CEO at ScholarHat

He is a renowned Speaker, Solution Architect, Mentor, and 11-time Microsoft MVP (2016–2026). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.

Keep learning

Courses that go with this tutorial