Blog Post

Azure Database Support Blog
2 MIN READ

How to cancel Azure SQL Database Import or Export operation

Yochanan_Rachamim's avatar
Nov 26, 2020

Purpose

This article help you cancel ongoing import or export operation on Azure SQL Database. 

 

Step by Step guidance: 

  1. Open new PowerShell window, you may use cloud shell on Azure portal as well by clicking the cloud shell button at the top right at your portal screen
  2.  

  3. Copy and paste the following PowerShell code and execute it - it will create a function for the current PowerShell session
  4. function Cancel-AzSQLImportExportOperation
    {
        param
        (
            [parameter(Mandatory=$true)][string]$ResourceGroupName
            ,[parameter(Mandatory=$true)][string]$ServerName
            ,[parameter(Mandatory=$true)][string]$DatabaseName
        )
    
        $Operation = Get-AzSqlDatabaseActivity -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName | Where-Object {($_.Operation -like "Export*" -or $_.Operation -like "Import*") -and $_.State -eq "InProgress"}
        
        if(-not [string]::IsNullOrEmpty($Operation))
        {
            do
            {
                Write-Host -ForegroundColor Cyan ("Operation " + $Operation.Operation + " with OperationID: " + $Operation.OperationId + " is now " + $Operation.State)
                $UserInput = Read-Host -Prompt "Should I cancel this operation? (Y/N)"
            } while($UserInput -ne "Y" -and $UserInput -ne "N")
    
            if($UserInput -eq "Y")
            { 
                "Canceling operation"
                Stop-AzSqlDatabaseActivity -ResourceGroupName $ResourceGroupName -ServerName $ServerName -DatabaseName $DatabaseName -OperationId $Operation.OperationId
            }
            else 
            {"Exiting without cenceling the operation"}
            
        }
        else
        {
            "No import or export operation is now running"
        }
    }
    
    Cancel-AzSQLImportExportOperation
  5. use the function 
    Cancel-AzSQLImportExportOperation​
    to cancel an Import or Export operation
  6. you need to provide the Resource Group name, Server name and Database name where the operation is currently running. 

 

I hope you find it useful. 

Thank you Roshna Nazir for pointing out this functionality.

if you have any feedback please do not hesitate to provide it in a comment below.

 

Yochanan.

 

 

 

Updated Feb 01, 2022
Version 8.0
  • Hi siredman - Thank you for your comment, Yes, this is expected, you should use your server name only without the suffix of ".database.windows.net"

     

  • When running the new function, please provide your server name and database name exactly like you would in any other Az Powershell command. For example: 

     

     

    Cancel-AzSqlImportExportOperation -ResourceGroupName myrg -ServerName myserver -DatabaseName mydatabase

     

     

    If you get an error like:

    Stop-AzSqlDatabaseActivity : The Resource
    'Microsoft.Sql/servers/myserver.database.windows.net/databases/mydatabase' under resource
    group 'myrg' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix

    The issue is you have entered your server URL (like "myserver.database.windows.net"), not the server name.