Jordan Roher
Jordan Roher

Reputation: 424

How to delete files in Azure Blob Storage by date using AzureCLI?

I'm trying to use the --if-unmodified-since flag on the delete-batch command to delete files, but Azure throws an error on my inline script.

I'm doing this in Azure DevOps on an Ubuntu VM using an azure-pipelines.yml file.

This command:

- task: AzureCLI@2
  displayName: Delete old files
  inputs:
    azureSubscription: 'Main subscription (xxx-xxx-xxx)'
    scriptType: pscore
    scriptLocation: inlineScript
    inlineScript: |
        az storage blob delete-batch -s $web --account-name mystorage --if-unmodified-since `date -d "1 days ago" '+%Y-%m-%dT%H:%MZ'`

Returns an Incomplete string token error, pointing at the end of the inline script.

enter image description here

Upvotes: 2

Views: 2123

Answers (1)

Sadiq Khoja
Sadiq Khoja

Reputation: 600

Backtick (`) is used for escaping characters in powershell, so I think it is easier to use $( command ) to execute nested commands [date -d "1 days ago" '+%Y-%m-%dT%H:%MZ' is a linux command]

Here the task, which worked for me

- task: AzureCLI@2
  inputs:
    azureSubscription: 'Visual Studio Professional Subscription(xxxxxx)'
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: 'az storage blob delete-batch -s images --account-name mystorage --if-unmodified-since $(date -d `"5 days ago`" ''+%Y-%m-%dT%H:%MZ'')'

Upvotes: 3

Related Questions