Jump to content

Bulk delete all the old jobs from the batch account


Recommended Posts

Guest akshitavijay
Posted

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:

 

 

 

 

 

[iCODE]Remove-AzBatchJob -Id "Job-000001" -BatchContext $Context[/iCODE]

 

 

 

 

 

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.

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...