How to delete SQL Server Database Mail history WITHOUT stored proceduresWhy use both TRUNCATE and DROP?Database security through stored proceduresHow to split tables without affecting all stored proceduresHow To Profile Stored ProceduresHow to keep history of SQL Server stored procedure revisions~2 Hours after a large insert SQL Server gets “I/O requests taking longer than 15 seconds to complete”High write latancy in temp dbSQL Server's “Total Server Memory” consumption stagnant for months with 64GB+ more availableSQL Server and TFS - How to rename stored proceduresTransaction log filling the drive if mirroring failsWhat Triggers sp_recompile in database without triggers, stored procedures, etc?
Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes
Jam with honey & without pectin has a saucy consistency always
Parallelized for loop in Bash
Should I worry about having my credit pulled multiple times while car shopping?
Fastest way from 10 to 1 with everyone in between
Why are backslashes included in this shell script?
Commencez à vous connecter -- I don't understand the phrasing of this
My parents claim they cannot pay for my college education; what are my options?
Idiom for 'person who gets violent when drunk"
How can religions without a hell discourage evil-doing?
Why did the AvroCar fail to fly above 3 feet?
Why does there seem to be an extreme lack of public trashcans in Taiwan?
Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?
Why is gun control associated with the socially liberal Democratic party?
Harley Davidson clattering noise from engine, backfire and failure to start
Am I allowed to determine tenets of my contract as a warlock?
How to represent jealousy in a cute way?
Can artificial satellite positions affect tides?
Shouldn't it take more energy to break CO2 compared to CO?
What game uses dice with compass point arrows, forbidden signs, explosions, arrows and targeting reticles?
Past vs. present tense when referring to a fictional character
What's the reason for the decade jump in the recent X-Men trilogy?
Approach sick days in feedback meeting
typeid("") != typeid(const char*)
How to delete SQL Server Database Mail history WITHOUT stored procedures
Why use both TRUNCATE and DROP?Database security through stored proceduresHow to split tables without affecting all stored proceduresHow To Profile Stored ProceduresHow to keep history of SQL Server stored procedure revisions~2 Hours after a large insert SQL Server gets “I/O requests taking longer than 15 seconds to complete”High write latancy in temp dbSQL Server's “Total Server Memory” consumption stagnant for months with 64GB+ more availableSQL Server and TFS - How to rename stored proceduresTransaction log filling the drive if mirroring failsWhat Triggers sp_recompile in database without triggers, stored procedures, etc?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This is really straight forward. The documented way to delete Database mail history in SQL server is to use the following two stored procedures:sysmail_delete_mailitems_sp
and sysmail_delete_log_sp
Great. But what happens when apparently billions of emails are stored in here taking up several hundred gigabytes in msdb
, and even the process of trying to clear only 100 days worth of email takes hours or days?
I don't want any history and I don't want to wait until I'm a senior citizen to clear the list of e-mail history. How do I truncate this history in one quick transaction?
So far my efforts at using the built in procedures have been futile - growing tempdb
and msdb
to terabytes in size and sitting "executing" for hours on end. Any attempt to cancel it results in the entire transaction being rolled back. It so far hasn't even been possible to count the number of emails that exist in the history - it's that bad.
sql-server
migrated from serverfault.com May 30 at 0:44
This question came from our site for system and network administrators.
add a comment |
This is really straight forward. The documented way to delete Database mail history in SQL server is to use the following two stored procedures:sysmail_delete_mailitems_sp
and sysmail_delete_log_sp
Great. But what happens when apparently billions of emails are stored in here taking up several hundred gigabytes in msdb
, and even the process of trying to clear only 100 days worth of email takes hours or days?
I don't want any history and I don't want to wait until I'm a senior citizen to clear the list of e-mail history. How do I truncate this history in one quick transaction?
So far my efforts at using the built in procedures have been futile - growing tempdb
and msdb
to terabytes in size and sitting "executing" for hours on end. Any attempt to cancel it results in the entire transaction being rolled back. It so far hasn't even been possible to count the number of emails that exist in the history - it's that bad.
sql-server
migrated from serverfault.com May 30 at 0:44
This question came from our site for system and network administrators.
add a comment |
This is really straight forward. The documented way to delete Database mail history in SQL server is to use the following two stored procedures:sysmail_delete_mailitems_sp
and sysmail_delete_log_sp
Great. But what happens when apparently billions of emails are stored in here taking up several hundred gigabytes in msdb
, and even the process of trying to clear only 100 days worth of email takes hours or days?
I don't want any history and I don't want to wait until I'm a senior citizen to clear the list of e-mail history. How do I truncate this history in one quick transaction?
So far my efforts at using the built in procedures have been futile - growing tempdb
and msdb
to terabytes in size and sitting "executing" for hours on end. Any attempt to cancel it results in the entire transaction being rolled back. It so far hasn't even been possible to count the number of emails that exist in the history - it's that bad.
sql-server
This is really straight forward. The documented way to delete Database mail history in SQL server is to use the following two stored procedures:sysmail_delete_mailitems_sp
and sysmail_delete_log_sp
Great. But what happens when apparently billions of emails are stored in here taking up several hundred gigabytes in msdb
, and even the process of trying to clear only 100 days worth of email takes hours or days?
I don't want any history and I don't want to wait until I'm a senior citizen to clear the list of e-mail history. How do I truncate this history in one quick transaction?
So far my efforts at using the built in procedures have been futile - growing tempdb
and msdb
to terabytes in size and sitting "executing" for hours on end. Any attempt to cancel it results in the entire transaction being rolled back. It so far hasn't even been possible to count the number of emails that exist in the history - it's that bad.
sql-server
sql-server
asked May 29 at 21:06
AppleoddityAppleoddity
1163
1163
migrated from serverfault.com May 30 at 0:44
This question came from our site for system and network administrators.
migrated from serverfault.com May 30 at 0:44
This question came from our site for system and network administrators.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
One idea could be:
- script out
CREATE TABLE
for sysmail_attachments, sysmail_send_retries, sysmail_mailitems, and sysmail_log - start a transaction
- drop the tables in that order
- create the tables again using the script from 1.
- commit the transaction
- create a job that purges those tables regularly (like, once an hour, from the sounds of it), also in that order
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
add a comment |
Building on @Aaron Bertrand's answer. I decided to delete the foreign key constraints and truncate the tables and then recreate the constraints.
This is the script I used to clear the e-mail history and it took about 1 second to run total:
BEGIN TRANSACTION;
USE MSDB;
ALTER TABLE [dbo].[sysmail_attachments] DROP [FK_sysmail_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_attachments];
ALTER TABLE [dbo].[sysmail_send_retries] DROP [FK_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_send_retries];
TRUNCATE TABLE [dbo].[sysmail_mailitems];
TRUNCATE TABLE [dbo].[sysmail_log];
ALTER TABLE [dbo].[sysmail_attachments] WITH CHECK ADD CONSTRAINT [FK_sysmail_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_attachments] CHECK CONSTRAINT [FK_sysmail_mailitems_mailitem_id]
ALTER TABLE [dbo].[sysmail_send_retries] WITH CHECK ADD CONSTRAINT [FK_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_send_retries] CHECK CONSTRAINT [FK_mailitems_mailitem_id]
COMMIT;
After that, it was just a matter or shrinking (and setting the initial size of) the msdb
and tempdb
databases.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f239409%2fhow-to-delete-sql-server-database-mail-history-without-stored-procedures%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One idea could be:
- script out
CREATE TABLE
for sysmail_attachments, sysmail_send_retries, sysmail_mailitems, and sysmail_log - start a transaction
- drop the tables in that order
- create the tables again using the script from 1.
- commit the transaction
- create a job that purges those tables regularly (like, once an hour, from the sounds of it), also in that order
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
add a comment |
One idea could be:
- script out
CREATE TABLE
for sysmail_attachments, sysmail_send_retries, sysmail_mailitems, and sysmail_log - start a transaction
- drop the tables in that order
- create the tables again using the script from 1.
- commit the transaction
- create a job that purges those tables regularly (like, once an hour, from the sounds of it), also in that order
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
add a comment |
One idea could be:
- script out
CREATE TABLE
for sysmail_attachments, sysmail_send_retries, sysmail_mailitems, and sysmail_log - start a transaction
- drop the tables in that order
- create the tables again using the script from 1.
- commit the transaction
- create a job that purges those tables regularly (like, once an hour, from the sounds of it), also in that order
One idea could be:
- script out
CREATE TABLE
for sysmail_attachments, sysmail_send_retries, sysmail_mailitems, and sysmail_log - start a transaction
- drop the tables in that order
- create the tables again using the script from 1.
- commit the transaction
- create a job that purges those tables regularly (like, once an hour, from the sounds of it), also in that order
answered May 30 at 1:19
Aaron Bertrand♦Aaron Bertrand
157k18307514
157k18307514
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
add a comment |
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
Thank you for your answer. I had already decided to try a similar method of dropping the foreign key constraints and truncating the tables. It seems to have worked properly. Take a look and see if you see any issues in my answer. I appreciate your help!
– Appleoddity
May 30 at 5:37
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
@Appleoddity well, removing keys and truncating is a lot more work, obviously, and there is a tiny bit more logging involved, and some more too with the FK changes, but whatever floats your boat I guess.
– Aaron Bertrand♦
May 30 at 10:39
add a comment |
Building on @Aaron Bertrand's answer. I decided to delete the foreign key constraints and truncate the tables and then recreate the constraints.
This is the script I used to clear the e-mail history and it took about 1 second to run total:
BEGIN TRANSACTION;
USE MSDB;
ALTER TABLE [dbo].[sysmail_attachments] DROP [FK_sysmail_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_attachments];
ALTER TABLE [dbo].[sysmail_send_retries] DROP [FK_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_send_retries];
TRUNCATE TABLE [dbo].[sysmail_mailitems];
TRUNCATE TABLE [dbo].[sysmail_log];
ALTER TABLE [dbo].[sysmail_attachments] WITH CHECK ADD CONSTRAINT [FK_sysmail_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_attachments] CHECK CONSTRAINT [FK_sysmail_mailitems_mailitem_id]
ALTER TABLE [dbo].[sysmail_send_retries] WITH CHECK ADD CONSTRAINT [FK_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_send_retries] CHECK CONSTRAINT [FK_mailitems_mailitem_id]
COMMIT;
After that, it was just a matter or shrinking (and setting the initial size of) the msdb
and tempdb
databases.
add a comment |
Building on @Aaron Bertrand's answer. I decided to delete the foreign key constraints and truncate the tables and then recreate the constraints.
This is the script I used to clear the e-mail history and it took about 1 second to run total:
BEGIN TRANSACTION;
USE MSDB;
ALTER TABLE [dbo].[sysmail_attachments] DROP [FK_sysmail_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_attachments];
ALTER TABLE [dbo].[sysmail_send_retries] DROP [FK_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_send_retries];
TRUNCATE TABLE [dbo].[sysmail_mailitems];
TRUNCATE TABLE [dbo].[sysmail_log];
ALTER TABLE [dbo].[sysmail_attachments] WITH CHECK ADD CONSTRAINT [FK_sysmail_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_attachments] CHECK CONSTRAINT [FK_sysmail_mailitems_mailitem_id]
ALTER TABLE [dbo].[sysmail_send_retries] WITH CHECK ADD CONSTRAINT [FK_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_send_retries] CHECK CONSTRAINT [FK_mailitems_mailitem_id]
COMMIT;
After that, it was just a matter or shrinking (and setting the initial size of) the msdb
and tempdb
databases.
add a comment |
Building on @Aaron Bertrand's answer. I decided to delete the foreign key constraints and truncate the tables and then recreate the constraints.
This is the script I used to clear the e-mail history and it took about 1 second to run total:
BEGIN TRANSACTION;
USE MSDB;
ALTER TABLE [dbo].[sysmail_attachments] DROP [FK_sysmail_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_attachments];
ALTER TABLE [dbo].[sysmail_send_retries] DROP [FK_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_send_retries];
TRUNCATE TABLE [dbo].[sysmail_mailitems];
TRUNCATE TABLE [dbo].[sysmail_log];
ALTER TABLE [dbo].[sysmail_attachments] WITH CHECK ADD CONSTRAINT [FK_sysmail_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_attachments] CHECK CONSTRAINT [FK_sysmail_mailitems_mailitem_id]
ALTER TABLE [dbo].[sysmail_send_retries] WITH CHECK ADD CONSTRAINT [FK_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_send_retries] CHECK CONSTRAINT [FK_mailitems_mailitem_id]
COMMIT;
After that, it was just a matter or shrinking (and setting the initial size of) the msdb
and tempdb
databases.
Building on @Aaron Bertrand's answer. I decided to delete the foreign key constraints and truncate the tables and then recreate the constraints.
This is the script I used to clear the e-mail history and it took about 1 second to run total:
BEGIN TRANSACTION;
USE MSDB;
ALTER TABLE [dbo].[sysmail_attachments] DROP [FK_sysmail_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_attachments];
ALTER TABLE [dbo].[sysmail_send_retries] DROP [FK_mailitems_mailitem_id];
TRUNCATE TABLE [dbo].[sysmail_send_retries];
TRUNCATE TABLE [dbo].[sysmail_mailitems];
TRUNCATE TABLE [dbo].[sysmail_log];
ALTER TABLE [dbo].[sysmail_attachments] WITH CHECK ADD CONSTRAINT [FK_sysmail_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_attachments] CHECK CONSTRAINT [FK_sysmail_mailitems_mailitem_id]
ALTER TABLE [dbo].[sysmail_send_retries] WITH CHECK ADD CONSTRAINT [FK_mailitems_mailitem_id] FOREIGN KEY([mailitem_id])
REFERENCES [dbo].[sysmail_mailitems] ([mailitem_id])
ON DELETE CASCADE
ALTER TABLE [dbo].[sysmail_send_retries] CHECK CONSTRAINT [FK_mailitems_mailitem_id]
COMMIT;
After that, it was just a matter or shrinking (and setting the initial size of) the msdb
and tempdb
databases.
answered May 30 at 5:34
AppleoddityAppleoddity
1163
1163
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f239409%2fhow-to-delete-sql-server-database-mail-history-without-stored-procedures%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown