Blog Post

Azure PaaS Blog
2 MIN READ

Bulk delete all the old jobs from the batch account

akshitavijay's avatar
akshitavijay
Icon for Microsoft rankMicrosoft
Aug 22, 2024

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task data; that is, if the Job contains Tasks which are still retained on Compute Nodes, the Batch service deletes those Tasks' working directories and all their contents. When a Delete Job request is received, the Batch service sets the Job to the deleting state. All update operations on a Job that is in deleting state will fail with status code 409 (Conflict), with additional information indicating that the Job is being deleted.

 

We can use below PowerShell command to delete one single job using job id:

 

 

 

Remove-AzBatchJob -Id "Job-000001" -BatchContext $Context

 

 

 

But if you have a large number of jobs and wants to delete them simultaneously, then you can refer below PowerShell command for the same:

 

 

# Replace with your Azure Batch account name, resource group, and subscription ID
$accountName = "yourBatchAccountName"
$resourceGroupName = "yourResourceGroupName"

# Authenticate to Azure
Connect-AzAccount

# Get the Batch account context
$batchContext = Get-AzBatchAccount -Name $accountName -ResourceGroupName $resourceGroupName

# Get all batch jobs with creation time before May 2024
# Replace the creation time date accordingly

$jobsForDelete = Get-AzBatchJob -BatchContext $batchContext | Where-Object {$_.CreationTime -lt "2024-05-01"}

# List the jobs
Write-Host "Jobs to be deleted:"
foreach ($job in $jobsForDelete) {
    Write-Host $job.Id
    # Write-Host "Deleting jobs..."
    Remove-AzBatchJob -Id $job.Id -BatchContext $batchContext -Force
}


 

 

 

 

The above script will delete all the jobs created before the creation date. You can accordingly modify the parameters as per your requirement.

 

Updated Sep 11, 2024
Version 2.0
No CommentsBe the first to comment