MS SQL Maintenance Plans
Maintenance Plans in MS SQL 2005 are a powerful and easy way to manage and backup your databases. In this article, I’m going to walk through 4 basic tasks that we run weekly on a client’s database. You’ll learn how to reorganize indexes, create a full backup (*.bak), shrink your database (shrinks both data and log files), and cleanup the backup directory.
Creating a Maintenance Plan
In SQL Server Management Studio, expand the Management folder, right-click Maintenance Plans and select New Maintenance Plan…
Give the Maintenance Plan a name and click OK. The new empty Maintenance Plan will open in Design Mode. All of the available tasks are in the left hand column Toolbox under Maintenance Plan Tasks.
To add any task to the maintenance plan, simply drag the task from the Toolbox into the design frame. Once the task is in the design frame, double-click to edit.
Reorganize Index Task
The reorganize index task updates table and view indexes so the search order is more efficient. The task dialogue is pretty simple. Use the Database(s) drop down to select one or more databases. Then use the Object drop down to select whether Tables, Views, or Both should have their indexes reorganized. For this example, I’m going to reorganize both Tables and Views. When done, click OK.
Back Up Database Task
The Back Up Database Task creates a Full, Differential, or Transaction Log backup. In this example, I’m going to create a Full backup which will create a *.bak file. This task dialogue is also pretty simple. Set the Backup type to Full and then select the Database(s) to backup. Jump down to the bottom third of the dialogue and select the Create a backup file for every database radio button. Then browse to the folder where the backup files are to be saved. When done, click OK.
Join the Reorganize Index Task to the Backup Database Task by dragging the green arrow between the two. Once the two tasks are joined, right-click the arrow and select Completion.
Shrink Database Task
The Shrink Database Task should come after the back up task because the shrink cannot be executed on a database until a Full backup has been created. In the task dialogue, select the Database(s) and just accept the defaults. When done, click OK. If you’ve noticed your log files getting out of hand, the shrink task will bring the file size down. In a recent test, my active log file went from over 2GB to just over 200MB.
Join the Back Up Database Task to the Shrink Database Task, and set the type to Completion.
Maintenance Cleanup Task
As the last task in the Maintenance Plan, I’m going to cleanup the backup directory by removing any *.bak files that are older than 2 weeks. If you’re doing this type of maintenance, you’ll want to make sure you’ve got some sort of server backup that keeps a longer history of your *.bak files off-server.
Open the task dialogue and skip down to the Search folder and delete files based on extension. Navigate to the backup folder that was set in the Back Up Database Task. Then set the file extension to bak. Note the extension is simply bak. You don’t need to include a . (dot). Skip to the bottom of the dialogue and set the age of the deleted files to 2 weeks. When done, click OK.
Join the Shrink Database Task to the Maintenance Cleanup Task, and set the type to Success.
Execute the Maintenance Plan
Save the maintenance plan. From the Object Explorer, right click the new plan and select Execute to test it. If everything runs without error, you can set the plan to run on a schedule.
On the design mode screen in the Subplan grid, click on the calendar icon and set the schedule for executing the plan. When done, click OK.
That’s it. The plan will now run as a scheduled task. Not only does the plan backup the database, but it’s also reorganizing the indexes, shrinking the database and cleaning up the backup directory. Best of all, once it’s setup, the plan executes without your intervention and at a time that is best for your system.

The agent and the maintenance plans are one of favourite things about working with SQL Server, whenever I work for someone who only has the Express edition I always miss it as it’s such an integral tool.
Thanks for the tutorial, it’ll provide very helpfull as it’s something which I think is often missed by new developers.
Rob
Comment by Robert Rawlins — September 10, 2009 @ 5:12 pm
Perry,
Great tutorial – makes life easier for dba of all levels
Will this maintenance plan shrink the logfiles? I did not see a dbcc shrink file when viewing the sql statemetns in object explorer?
Your thoughts,
jrwalker1048
Comment by jrwalker1048 — February 11, 2010 @ 4:18 pm
@jrwalker. Thanks for taking the time to comment.
The shrink task will indeed execute the DBCC SHRINKDATABASE statement http://msdn.microsoft.com/en-us/library/ms188432.aspx
Not sure why you didn’t see that in object explorer. If you view the Job History of your maintenance plan under SQL Server Agent, you should see “Executing query ‘DBCC SHRINKDATABASE…” within the job details.
Comment by Perry Woodin — February 11, 2010 @ 4:47 pm
Hi
I have created a simple management plan where all databases are backed up and then there is a deletion of files older than 2 days.
The backup process is working fine and the files are created, however the deletion of older files is not running so I end up deleting these manually.
I can’t see any difference in the setup you have illustrated above.
Any Help??
Comment by Jeet — March 4, 2010 @ 6:04 am
@Jeet. Thanks for writing. Double-check your Cleanup Task. I’ve run into this issue when using the incorrect File Extension. Make sure the File Extension is just bak (not .bak, “bak”, or BAK).
Comment by Perry Woodin — March 4, 2010 @ 9:53 am
Thanks for your quick response!!
The file extension I was using is bak so that is not an issue.
However I did notice that my backup folder was set to Read Only which I changed, also I chose the option in SQL Task wizard to include first level sub folders. This has worked and bak files older than 2 days were deleted.
Comment by Jeet — March 5, 2010 @ 5:00 am
Hello,
I just wanted to jump a bit on some of the comments.
- 1. shrinking any file is not really a good practice as you would fragment physically your file. You should estimate what would be a good data and log file initial size and growth (ideally restrict them to a certain size). Physical fragmentation and poor performing file growth is sometimes a major bottleneck for large batch processes.
- 2. You can experience having difficulties in deleting files from cleanup task (or xp_deletefiles) also because of permissions. It is sometimes helpful to set up credential and proxy account to run the maintenance plan within sql server job.
- 3. Reorganizing indexing can take a lot of ressources. however it does not block any processes even in standard edition. If you are on entreprise version, I would probably advise for a rebuild after a certain level of fragmentation. Optimizing and rationalizing your index management can be very handy and remove unnecessary operations, releasing IO and CPU ressources for your other important processes.
- 4. Maintenance plan is quite handy indeed, especially to maintain within jobs. However most of the tasks within maintenance plan can also be done through T-SQL. If you feel more comfortable or you like to set up a better rationalized maintenance plan, it is sometimes usefull to go with T-SQL.
Comment by Clement — June 28, 2010 @ 1:38 pm