Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:38:20+00:00 2026-06-08T18:38:20+00:00

Im using SQL Server 2012 and I have a case where I have to

  • 0

Im using SQL Server 2012 and I have a case where I have to delete sql jobs. I found two approaches which are as follows:

Approach 1

DECLARE @jobId binary(16)  

WHILE (1=1)
BEGIN
    SET @jobId = NULL
    SELECT TOP 1 @jobId = job_id FROM msdb.dbo.sysjobs WHERE (name like N'Abc%') 

    IF @@ROWCOUNT = 0
        BREAK

    IF (@jobId IS NOT NULL) 
    BEGIN     
        EXEC msdb.dbo.sp_delete_job @jobId 
    END 
END

Approach 2

DECLARE @listStr VARCHAR(MAX)=null

SELECT @listStr = COALESCE(@listStr+'exec msdb.dbo.sp_delete_job ' ,'') + '''' + convert(varchar(max),job_id) + '''; '
FROM msdb.dbo.sysjobs WHERE (name like N'$(TestPublisherServer)-$(TestPublisherDB)%') 

IF @listStr is not null
BEGIN 
    PRINT 'exec msdb.dbo.sp_delete_job ' + @listStr
    EXEC ('exec msdb.dbo.sp_delete_job ' + @listStr)
END

Both the approaches will delete the jobs, but I want to know which is the best way or you can suggest me more efficient or correct ways to delete the jobs.

And one more question is, do we have to stop/disable the job before deleting it.

TIA
Harsha

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:38:21+00:00Added an answer on June 8, 2026 at 6:38 pm

    I would use approach 1 because it is just simple and effective. There is no need to disable the job before. If you delete a job while it is running, the job will terminate with an error, so check if thats requirement for you.

    Way to handle this problem(posted by OP):

    IF exists(
        select 1 from msdb.dbo.sysjobactivity activity 
        where activity.run_Requested_date is not null 
               and activity.stop_execution_date is null 
               and activity.job_id = @jobId) 
     exec msdb.dbo.sp_stop_job @job_id = @jobId
    

    I’m not sure if you are worried about the schedule to stay. Depending on how @delete_unused_schedule is set, sp_delete_job will delete it by default.

    See the stored procedure comment

    @delete_unused_schedule   BIT   = 1     -- For backward compatibility schedules are deleted by default if they are not 
                                            -- being used by another job. With the introduction of reusable schedules in V9 
                                            -- callers should set this to 0 so the schedule will be preserved for reuse.
    

    So this option will remain you flexible, but i think you will have no need for the schedule because you probably want to clean up your system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2012 Express. I have a table which represents key/value pairs. keycol
I'm using SQL Server 2008 R2 and I have a database which contains two
We have an issue upgrading to SQL Server 2012. I am using the following
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
Using SQL Server 2008 (R2). I have two tables in different databases (with same
Using SQL Server 2K8 R2, I have two related tables - Member and Questionnaire.
I am using SQL Server 2012 I have a table called AMOUNTS and a
Im using sql server 2012 and its replication feature. Here im trying to manually
I'm using SQL Server 2012 and trying to implement transactional replication. Im using the
I am using a JDBC driver for Microsoft SQL Server 2012 (sqljdbc_4.0) and although

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.