cortex_cleanup_jobs
Delete Cortex jobs by status or age. Use dry run to preview matching jobs before deletion.
Instructions
Delete multiple jobs by status or age. Useful for cleaning up failed or old jobs.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| status | No | Delete jobs with this status | |
| olderThanDays | No | Delete jobs older than this many days | |
| dryRun | No | If true (default), only count matching jobs without deleting them | |
| limit | No | Maximum jobs to process (default: 100) |
Implementation Reference
- src/tools/jobs.ts:300-403 (handler)The async handler function for cortex_cleanup_jobs that executes the tool logic. It searches for matching jobs by status and/or age, supports dry-run mode, and performs batch deletion with Promise.allSettled.
async ({ status, olderThanDays, dryRun, limit }) => { try { if (!status && !olderThanDays) { return { content: [ { type: "text" as const, text: "Please specify at least one filter: status or olderThanDays.", }, ], isError: true, }; } const must: Record<string, unknown>[] = []; if (status) { must.push({ _field: "status", _value: status }); } const query: Record<string, unknown> = must.length > 0 ? must.length === 1 ? must[0] : { _and: must } : { _field: "status", _value: "*" }; const jobs = await client.searchJobs({ query, range: `0-${limit}`, sort: ["-createdAt"], }); let matchingJobs = jobs; if (olderThanDays) { const cutoff = Date.now() - olderThanDays * 24 * 60 * 60 * 1000; matchingJobs = jobs.filter( (j) => (j.createdAt ?? 0) < cutoff, ); } if (dryRun) { return { content: [ { type: "text" as const, text: JSON.stringify( { dryRun: true, matchingJobs: matchingJobs.length, message: `Found ${matchingJobs.length} jobs matching criteria. Set dryRun=false to delete them.`, jobs: matchingJobs.map((j) => ({ id: j.id, status: j.status, analyzer: j.analyzerName, data: j.data, createdAt: j.createdAt ? new Date(j.createdAt).toISOString() : undefined, })), }, null, 2, ), }, ], }; } // Actually delete const results = await Promise.allSettled( matchingJobs.map((j) => client.deleteJob(j.id)), ); const deleted = results.filter((r) => r.status === "fulfilled").length; const failed = results.filter((r) => r.status === "rejected").length; return { content: [ { type: "text" as const, text: JSON.stringify( { dryRun: false, deleted, failed, total: matchingJobs.length, message: `Deleted ${deleted}/${matchingJobs.length} jobs.${failed > 0 ? ` ${failed} deletions failed.` : ""}`, }, null, 2, ), }, ], }; } catch (error) { return { content: [ { type: "text" as const, text: `Error cleaning up jobs: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }, - src/tools/jobs.ts:277-299 (schema)Zod schema definitions for the cortex_cleanup_jobs tool parameters: status (enum: Failure/Deleted/Success/Waiting/InProgress), olderThanDays (int, min 1), dryRun (boolean, default true), limit (int, min 1, max 500, default 100).
{ status: z .enum(["Failure", "Deleted", "Success", "Waiting", "InProgress"]) .optional() .describe("Delete jobs with this status"), olderThanDays: z .number() .int() .min(1) .optional() .describe("Delete jobs older than this many days"), dryRun: z .boolean() .default(true) .describe("If true (default), only count matching jobs without deleting them"), limit: z .number() .int() .min(1) .max(500) .default(100) .describe("Maximum jobs to process (default: 100)"), }, - src/tools/jobs.ts:274-275 (registration)Registration of the tool via server.tool("cortex_cleanup_jobs", ...) inside the registerJobTools function.
server.tool( "cortex_cleanup_jobs", - src/tools/jobs.ts:5-8 (registration)The registerJobTools function that exports all job-related tools, including cortex_cleanup_jobs.
export function registerJobTools( server: McpServer, client: CortexClient, ): void {