pythonredisarq

How do I delete a job from ARQ Redis


I'm trying to remove a scheduled job from ARQ (Python Redis job queue), but none of my approaches work. Here's what I've attempted:

Attempt: 1

arq_redis = await get_arq_redis()
await arq_redis.delete_job(schedule.job_id)

Attempt: 2

arq_redis = await get_arq_redis()
queued_jobs = await arq_redis.queued_jobs()

for job in queued_jobs:
    if job.job_id == my_desired_job_id:
        await job.delete()

Attempt: 3

for job in queued_jobs:
    if job.job_id == schedule.job_id:
        await job.abort()

Attempt: 4

for job_def in queued_jobs:
    if job_def.job_id == schedule.job_id:
        job = await arq_redis.get_job(job_def.job_id)
        await job.abort()

Solution

  • How to Properly Remove a Scheduled Job from ARQ

    The issue you're encountering stems from working with JobDef objects (from queued_jobs()) which are essentially read-only. To properly abort a scheduled job in ARQ, you need to work with Job objects instead.

    The Solution

    Here's the correct approach:

    arq_redis = await get_arq_redis()
    job = Job(job_id=your_job_id, redis=arq_redis)
    status = await job.abort()
    

    Why Your Attempts Didn't Work

    1. Attempt 1: delete_job() isn't the correct method for aborting scheduled jobs
    2. Attempts 2-4: You're trying to perform operations on JobDef objects from queued_jobs(), which don't support direct modification

    Complete Working Example

    from arq.jobs import Job
    
    async def cancel_scheduled_job(job_id: str):
        arq_redis = await get_arq_redis()
        job = Job(job_id=job_id, redis=arq_redis)
        return await job.abort()
    
    # Usage:
    success = await cancel_scheduled_job("your-job-id-here")
    

    Important Notes

    1. The abort() method returns a boolean indicating success
    2. Make sure the job hasn't already started executing - abort only works for queued/scheduled jobs
    3. For recurring jobs, you might need to remove them from the Redis store completely

    This approach properly creates a Job instance that can interact with the Redis queue to abort the scheduled task.