Home > BI, Code, Tips > Script to Delete a SQL Agent Job

Script to Delete a SQL Agent Job

I have been in under the impression that you could not drop SQL Agent Jobs without being able to look at the properties and being able to get the JOB ID, since you need the jobid to pass to the sp_delete_job function.  Because of that, whenever I wanted to change a Job, I always had to incude a manual step to log onto the server, right click and delete the job.

However, I found a simple script to automate this, so now I can just include this in my script that will recreate the job.  Simple… should have known.
USE [msdb]
GO
DECLARE
@jobid sysname;
IF
EXISTS (SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = N'EXACT_JOB_NAME')
BEGIN
SELECT
@jobid = job_id FROM msdb.dbo.sysjobs_view WHERE name = N'EXACT_JOB_NAME'
EXEC  msdb.dbo.sp_delete_job @job_id=@jobid
END 

VN:F [1.9.1_1087]

Rating: 4.0/5 (2 votes cast)
VN:F [1.9.1_1087]
Rating: 0 (from 0 votes)
Script to Delete a SQL Agent Job, 4.0 out of 5 based on 2 ratings
Categories: BI, Code, Tips Tags: , , , ,