<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Delete Backup History &#8211; Cleanup Backup History</title>
	<atom:link href="http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Sun, 12 Feb 2012 09:22:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: JG</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-162286</link>
		<dc:creator><![CDATA[JG]]></dc:creator>
		<pubDate>Fri, 26 Aug 2011 14:46:02 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-162286</guid>
		<description><![CDATA[I tweaked the System SP to make it more efficient.  I ran into too many blockings and lockings with table data types.  I used good old temp tables and indexed them.

Also I am deleting only the TLog backups here.  You can change it to do both by removing the where clause for &#039;L&#039;

I ran it giving it 15 days at a time.  


USE [msdb]
GO
/****** Object:  StoredProcedure [dbo].[spDeleteBackuphistoryTLogs]    Script Date: 08/26/2011 09:13:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER   PROCEDURE [dbo].[spDeleteBackuphistoryTLogs]
  @oldest_date datetime
AS
BEGIN
  SET NOCOUNT ON




  SELECT DISTINCT backup_set_id INTO #TempBackupSetID
  FROM msdb.dbo.backupset
  WHERE type = &#039;L&#039; and
		backup_finish_date &lt; @oldest_date

	CREATE CLUSTERED INDEX CID_TBSI ON #TempBackupSetID (backup_set_id)


  SELECT DISTINCT media_set_id INTO #TempMediaSetID
  FROM msdb.dbo.backupset
  WHERE type = &#039;L&#039; and
		backup_finish_date  0)
    GOTO Quit

  DELETE FROM msdb.dbo.backupfilegroup
  WHERE backup_set_id IN (SELECT backup_set_id
                          FROM #TempBackupSetID)
  IF (@@error &gt; 0)
    GOTO Quit

  DELETE FROM msdb.dbo.restorefile
  WHERE restore_history_id IN (SELECT restore_history_id
                               FROM #TempRestoreHistoryID)
  IF (@@error &gt; 0)
    GOTO Quit

  DELETE FROM msdb.dbo.restorefilegroup
  WHERE restore_history_id IN (SELECT restore_history_id
                               FROM #TempRestoreHistoryID)
  IF (@@error &gt; 0)
    GOTO Quit

  DELETE FROM msdb.dbo.restorehistory
  WHERE restore_history_id IN (SELECT restore_history_id
                               FROM #TempRestoreHistoryID)
  IF (@@error &gt; 0)
    GOTO Quit

  DELETE FROM msdb.dbo.backupset
  WHERE backup_set_id IN (SELECT backup_set_id
                          FROM #TempBackupSetID)
  IF (@@error &gt; 0)
    GOTO Quit

  DELETE msdb.dbo.backupmediafamily
  FROM msdb.dbo.backupmediafamily bmf
  WHERE bmf.media_set_id IN (SELECT media_set_id
                             FROM #TempMediaSetID)
    AND ((SELECT COUNT(*)
          FROM msdb.dbo.backupset
          WHERE media_set_id = bmf.media_set_id) = 0)
  IF (@@error &gt; 0)
    GOTO Quit

  DELETE msdb.dbo.backupmediaset
  FROM msdb.dbo.backupmediaset bms
  WHERE bms.media_set_id IN (SELECT media_set_id
                             FROM #TempMediaSetID)
    AND ((SELECT COUNT(*)
          FROM msdb.dbo.backupset
          WHERE media_set_id = bms.media_set_id) = 0)

	DROP TABLE #TempBackupSetID
	DROP TABLE #TempMediaSetID
	DROP TABLE #TempRestoreHistoryID

  IF (@@error &gt; 0)
    GOTO Quit

  COMMIT TRANSACTION
  RETURN

Quit:
  ROLLBACK TRANSACTION

END]]></description>
		<content:encoded><![CDATA[<p>I tweaked the System SP to make it more efficient.  I ran into too many blockings and lockings with table data types.  I used good old temp tables and indexed them.</p>
<p>Also I am deleting only the TLog backups here.  You can change it to do both by removing the where clause for &#8216;L&#8217;</p>
<p>I ran it giving it 15 days at a time.  </p>
<p>USE [msdb]<br />
GO<br />
/****** Object:  StoredProcedure [dbo].[spDeleteBackuphistoryTLogs]    Script Date: 08/26/2011 09:13:26 ******/<br />
SET ANSI_NULLS ON<br />
GO<br />
SET QUOTED_IDENTIFIER OFF<br />
GO<br />
ALTER   PROCEDURE [dbo].[spDeleteBackuphistoryTLogs]<br />
  @oldest_date datetime<br />
AS<br />
BEGIN<br />
  SET NOCOUNT ON</p>
<p>  SELECT DISTINCT backup_set_id INTO #TempBackupSetID<br />
  FROM msdb.dbo.backupset<br />
  WHERE type = &#8216;L&#8217; and<br />
		backup_finish_date &lt; @oldest_date</p>
<p>	CREATE CLUSTERED INDEX CID_TBSI ON #TempBackupSetID (backup_set_id)</p>
<p>  SELECT DISTINCT media_set_id INTO #TempMediaSetID<br />
  FROM msdb.dbo.backupset<br />
  WHERE type = &#039;L&#039; and<br />
		backup_finish_date  0)<br />
    GOTO Quit</p>
<p>  DELETE FROM msdb.dbo.backupfilegroup<br />
  WHERE backup_set_id IN (SELECT backup_set_id<br />
                          FROM #TempBackupSetID)<br />
  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  DELETE FROM msdb.dbo.restorefile<br />
  WHERE restore_history_id IN (SELECT restore_history_id<br />
                               FROM #TempRestoreHistoryID)<br />
  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  DELETE FROM msdb.dbo.restorefilegroup<br />
  WHERE restore_history_id IN (SELECT restore_history_id<br />
                               FROM #TempRestoreHistoryID)<br />
  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  DELETE FROM msdb.dbo.restorehistory<br />
  WHERE restore_history_id IN (SELECT restore_history_id<br />
                               FROM #TempRestoreHistoryID)<br />
  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  DELETE FROM msdb.dbo.backupset<br />
  WHERE backup_set_id IN (SELECT backup_set_id<br />
                          FROM #TempBackupSetID)<br />
  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  DELETE msdb.dbo.backupmediafamily<br />
  FROM msdb.dbo.backupmediafamily bmf<br />
  WHERE bmf.media_set_id IN (SELECT media_set_id<br />
                             FROM #TempMediaSetID)<br />
    AND ((SELECT COUNT(*)<br />
          FROM msdb.dbo.backupset<br />
          WHERE media_set_id = bmf.media_set_id) = 0)<br />
  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  DELETE msdb.dbo.backupmediaset<br />
  FROM msdb.dbo.backupmediaset bms<br />
  WHERE bms.media_set_id IN (SELECT media_set_id<br />
                             FROM #TempMediaSetID)<br />
    AND ((SELECT COUNT(*)<br />
          FROM msdb.dbo.backupset<br />
          WHERE media_set_id = bms.media_set_id) = 0)</p>
<p>	DROP TABLE #TempBackupSetID<br />
	DROP TABLE #TempMediaSetID<br />
	DROP TABLE #TempRestoreHistoryID</p>
<p>  IF (@@error &gt; 0)<br />
    GOTO Quit</p>
<p>  COMMIT TRANSACTION<br />
  RETURN</p>
<p>Quit:<br />
  ROLLBACK TRANSACTION</p>
<p>END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TonyS.</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-62433</link>
		<dc:creator><![CDATA[TonyS.]]></dc:creator>
		<pubDate>Mon, 08 Mar 2010 17:24:37 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-62433</guid>
		<description><![CDATA[One key problem with this procedure and other similar ones is that they don&#039;t yield to other running processes. I had to use this procedure in emergency and it can cause locking and blocking unless you have exclusive access to the tables used in the stored procedure. 

This is because often many millions of rows being deleted if you are using log shipping of several databases for example that performs a frequent log backup for each one. 

Maybe MS will rewrite them to give way in the next release.

Tony S.]]></description>
		<content:encoded><![CDATA[<p>One key problem with this procedure and other similar ones is that they don&#8217;t yield to other running processes. I had to use this procedure in emergency and it can cause locking and blocking unless you have exclusive access to the tables used in the stored procedure. </p>
<p>This is because often many millions of rows being deleted if you are using log shipping of several databases for example that performs a frequent log backup for each one. </p>
<p>Maybe MS will rewrite them to give way in the next release.</p>
<p>Tony S.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: teenu ranga</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-55054</link>
		<dc:creator><![CDATA[teenu ranga]]></dc:creator>
		<pubDate>Thu, 20 Aug 2009 12:37:20 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-55054</guid>
		<description><![CDATA[sir , 
           i have some problem , i have 10 system and one server
and  i want to delete all the search item and web history from server  will be it&#039;s possible...? if possible please send me reply 
it&#039;s n urgent...........




                                                                  teenu ranga 
                                                                    bikaner 
                                                                       rajasthan]]></description>
		<content:encoded><![CDATA[<p>sir ,<br />
           i have some problem , i have 10 system and one server<br />
and  i want to delete all the search item and web history from server  will be it&#8217;s possible&#8230;? if possible please send me reply<br />
it&#8217;s n urgent&#8230;&#8230;&#8230;..</p>
<p>                                                                  teenu ranga<br />
                                                                    bikaner<br />
                                                                       rajasthan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Narenndra</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-54014</link>
		<dc:creator><![CDATA[Narenndra]]></dc:creator>
		<pubDate>Thu, 23 Jul 2009 12:57:58 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-54014</guid>
		<description><![CDATA[Hi Pinal,

I have a doubt regarding the comments whenever a question is raised by some one hw can d solution known to d people apart from the questioner.

Thanks,
Narenndra]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I have a doubt regarding the comments whenever a question is raised by some one hw can d solution known to d people apart from the questioner.</p>
<p>Thanks,<br />
Narenndra</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Puneet</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-46815</link>
		<dc:creator><![CDATA[Puneet]]></dc:creator>
		<pubDate>Wed, 18 Feb 2009 02:30:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-46815</guid>
		<description><![CDATA[How we can check the details of back history using MSDB.]]></description>
		<content:encoded><![CDATA[<p>How we can check the details of back history using MSDB.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nils</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-44221</link>
		<dc:creator><![CDATA[Nils]]></dc:creator>
		<pubDate>Fri, 14 Nov 2008 12:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-44221</guid>
		<description><![CDATA[Hi. 
I only wanted to mention if you&#039;re using a German MS-SQL Server you got to replace the parameter 101 with 104 to get the correct input-Format for the sp_delete_backuphistory procedure.
i.e. SET @DaysToKeepHistory = CONVERT(VARCHAR(10), DATEADD(dd, -30, GETDATE()), 104)
regards.]]></description>
		<content:encoded><![CDATA[<p>Hi.<br />
I only wanted to mention if you&#8217;re using a German MS-SQL Server you got to replace the parameter 101 with 104 to get the correct input-Format for the sp_delete_backuphistory procedure.<br />
i.e. SET @DaysToKeepHistory = CONVERT(VARCHAR(10), DATEADD(dd, -30, GETDATE()), 104)<br />
regards.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: richa</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-44200</link>
		<dc:creator><![CDATA[richa]]></dc:creator>
		<pubDate>Thu, 13 Nov 2008 10:13:48 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-44200</guid>
		<description><![CDATA[hii pinal
is it possible to restore or get back  deleted backup history............
regards]]></description>
		<content:encoded><![CDATA[<p>hii pinal<br />
is it possible to restore or get back  deleted backup history&#8230;&#8230;&#8230;&#8230;<br />
regards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brent Ozar</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comment-44158</link>
		<dc:creator><![CDATA[Brent Ozar]]></dc:creator>
		<pubDate>Tue, 11 Nov 2008 14:45:17 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1491#comment-44158</guid>
		<description><![CDATA[Also, be aware that if you don&#039;t purge history regularly, you can end up with a really large MSDB - even just several gigs can give you performance problems when deleting history.  I&#039;ve run into servers that had to take an outage just to delete the history because it&#039;d grown so large.]]></description>
		<content:encoded><![CDATA[<p>Also, be aware that if you don&#8217;t purge history regularly, you can end up with a really large MSDB &#8211; even just several gigs can give you performance problems when deleting history.  I&#8217;ve run into servers that had to take an outage just to delete the history because it&#8217;d grown so large.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

