I am using sql Server 2008 R2.
We are using below query for reducing the msdb size.
use msdb exec sp_delete_backuphistory '01-MAR-2020'
When i execute this query in server whose msdb size is 15GB no rows were deleted.
Some testing server with lesser rows it worked fine.
Why is it not working ? How to reduce the msdb size is there any other method?
Best Answer
Delete Old Backup History
You might want to have a look at this stored procedure I created some time back. Instead of entering a date it gets the current date and goes 1080 days (default) back in time and starts deleting the backup history in steps of 1 (default) up until 180 days (default).
With a large msdb database you might have a lot of info that takes a long time to delete. Entering a simple date using the
sp_delete_backuphistory
procedure can cause issues, because it has to go a long way back in time to delete the information from the various tables.My stored procedure is just a wrapper which automates the task of deleting old backup history using the built-in
sp_delete_backuphistory
(Microsoft | SQL Docs) procedure. The advantage of my procedure is that you don't have to manually enter dates to clean up the history in large msdb database, but can use days instead and can turn on transactional handling.Once you have the procedure in your msdb database, then run the query via:
The stored procedure will then run with the default values:
You can change these values to meet your requirements. For example:
If you want lower values than the defaults, then you will have to modify the
@i..._CONST
values in the stored procedure, as the defaults are the lowest values possible.If you want to see what happens, then set the @iDebug parameter (bitwise):
E.g. turning on
@iDebug = 4
will delete the history in transactions which can reduce the overall impact on the msdb database.Example Run
Running the following command:
Produces the following output:
This should clear up old backup information in the msdb database.
However, it will not shrink the msdb database. That's a different issue, which can be achieved with ...
DBCC SHRINKFILE
The
DBCC SHRINKFILE
(Microsoft | SQL Docs) command can be used to reclaim space that was once used in a database. It is not recommended for general use, as a database will normally reclaim the released space again in most cases, but seeing as you have a rather large msdb database....Please be aware that if the msdb database is under pressure that you might not be able to actually shrink the database in one go.
You might not even be able to shrink the database down to 98 MB. If you right click the msdb database and then select:
TASK... | Shrink ... | Files ...
you will be presented with a dialog that will tell you the minimum size, that the database file(s) can be shrunk to. Take that value and add it to the second command:Good luck.