SQL Agent History Retention and Cleanup Scripts
By Tom Nonmacher
The SQL Server Agent is an integral part of managing and monitoring SQL Server. It provides an automated way to schedule jobs, monitor SQL Server, and process alerts. However, over time, the history of these jobs can accumulate and consume significant database storage. In this blog post, we will delve into the ways that you can manage your SQL Agent History and provide some helpful cleanup scripts.
First, let us look at SQL Server 2016 and 2017. SQL Server provides a system stored procedure named sp_purge_jobhistory that can be used to delete job history. The procedure takes the job name or job id as the parameter. It can be used as follows:
-- T-SQL code to purge job history
EXEC msdb.dbo.sp_purge_jobhistory @job_name = N'MyJobName';
For MySQL 5.7, there is no built-in procedure for purging job history as there is in SQL Server. However, we can write a simple script to delete old entries from the event scheduler's history. Here is an example:
-- MySQL code to delete old events
DELETE FROM mysql.event WHERE created < DATE_SUB(NOW(), INTERVAL 1 YEAR);
In the case of DB2 11.1, we can use the ADMIN_TASK_REMOVE procedure to remove completed administrative tasks from the history. This includes both one-time and recurring tasks. Here is an example:
-- DB2 code to remove old tasks
CALL SYSPROC.ADMIN_TASK_REMOVE('MyTaskName');
If you are using Azure SQL, you can also use the sp_purge_jobhistory system stored procedure to delete job history. However, keep in mind that in Azure SQL, the SQL Agent is replaced by the Elastic Job Agent. Here is how you can use the procedure in Azure SQL:
-- T-SQL code to purge job history in Azure
EXEC msdb.dbo.sp_purge_jobhistory @job_name = N'MyAzureJobName';
As a final note, remember to consider the implications of removing job history. Although it saves storage space, it also means that you lose the ability to review past job performance and issues. Therefore, it is recommended to only remove job history that is no longer necessary for your review or audit processes.
In conclusion, managing SQL Agent History is crucial to maintain the health and performance of your SQL Server. Depending on the SQL technology you are using, you can leverage built-in stored procedures or write custom scripts to automate the cleanup process. Remember to test these scripts in a non-production environment before implementing them in a live scenario.
Check out the latest articles from all our sites:
- How to Take Advantage of Flash Sales at Grocery Stores [https://www.ethrift.net]
- A brief history of the Galveston Hurricane of 1900 [https://www.galvestonbeachy.com]
- How to Plant and Maintain Chokeberry Bushes [https://www.gardenhomes.org]
- New Query Store Enhancements in SQL Server 2022 [https://www.sqlsupport.org]
- Heat: Why My Laptop Is Cooking My Lap [https://www.SupportMyPC.com]
- The Best Months to Visit South Korea for Cherry Blossoms and Fall Colors [https://www.treasureholidays.com]
Privacy Policy for sqlsupport.org
Last updated: Feb 03, 2026
sqlsupport.org respects your privacy and is committed to protecting any personal information you may provide while using this website.
This Privacy Policy document outlines the types of information that are collected and recorded by sqlsupport.org and how we use it.
Information We Collect
- Internet Protocol (IP) addresses
- Browser type and version
- Pages visited
- Time and date of visits
- Referring URLs
- Device type
Cookies and Web Beacons
sqlsupport.org uses cookies to store information about visitors preferences and to optimize the users experience.
How We Use Your Information
- Operate and maintain our website
- Improve user experience
- Analyze traffic patterns
- Prevent fraudulent activity
Contact
Email: admin@sqlsupport.org