How to cancel Azure SQL Database Import or Export operation

Published 11-26-2020 03:15 PM 3,224 Views
Microsoft

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. Yochanan_MSFT_0-1606478478944.png

     

  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 -eq "ExportDatabase" -or $_.Operation -eq "ImportDatabase") -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"
        }
    }
  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.

 

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-1935001%22%20slang%3D%22en-US%22%3EHow%20to%20cancel%20Azure%20SQL%20Database%20Import%20or%20Export%20operation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1935001%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EPurpose%3C%2FSTRONG%3E%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20article%20help%20you%20cancel%20ongoing%20import%20or%20export%20operation%20on%20Azure%20SQL%20Database.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%20by%20Step%20guidance%3A%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EOpen%20new%20PowerShell%20window%2C%20you%20may%20use%20cloud%20shell%20on%20Azure%20portal%20as%20well%20by%20clicking%20the%20cloud%20shell%20button%20at%20the%20top%20right%20at%20your%20portal%20screen%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Yochanan_MSFT_0-1606478478944.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F236338i176D01F8A8564E8F%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Yochanan_MSFT_0-1606478478944.png%22%20alt%3D%22Yochanan_MSFT_0-1606478478944.png%22%20%2F%3E%3C%2FSPAN%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20class%3D%22lia-indent-padding-left-30px%22%3ECopy%20and%20paste%20the%20following%20PowerShell%20code%20and%20execute%20it%20-%20it%20will%20create%20a%20function%20for%20the%20current%20PowerShell%20session%3C%2FLI%3E%0A%3CLI%20class%3D%22lia-indent-padding-left-30px%22%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efunction%20Cancel-AzSQLImportExportOperation%0A%7B%0A%20%20%20%20param%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20%5Bparameter(Mandatory%3D%24true)%5D%5Bstring%5D%24ResourceGroupName%0A%20%20%20%20%20%20%20%20%2C%5Bparameter(Mandatory%3D%24true)%5D%5Bstring%5D%24ServerName%0A%20%20%20%20%20%20%20%20%2C%5Bparameter(Mandatory%3D%24true)%5D%5Bstring%5D%24DatabaseName%0A%20%20%20%20)%0A%0A%20%20%20%20%24Operation%20%3D%20Get-AzSqlDatabaseActivity%20-ResourceGroupName%20%24ResourceGroupName%20-ServerName%20%24ServerName%20-DatabaseName%20%24DatabaseName%20%7C%20Where-Object%20%7B(%24_.Operation%20-eq%20%22ExportDatabase%22%20-or%20%24_.Operation%20-eq%20%22ImportDatabase%22)%20-and%20%24_.State%20-eq%20%22InProgress%22%7D%0A%20%20%20%20%0A%20%20%20%20if(-not%20%5Bstring%5D%3A%3AIsNullOrEmpty(%24Operation))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20do%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20-ForegroundColor%20Cyan%20(%22Operation%20%22%20%2B%20%24Operation.Operation%20%2B%20%22%20with%20OperationID%3A%20%22%20%2B%20%24Operation.OperationId%20%2B%20%22%20is%20now%20%22%20%2B%20%24Operation.State)%0A%20%20%20%20%20%20%20%20%20%20%20%20%24UserInput%20%3D%20Read-Host%20-Prompt%20%22Should%20I%20cancel%20this%20operation%3F%20(Y%2FN)%22%0A%20%20%20%20%20%20%20%20%7D%20while(%24UserInput%20-ne%20%22Y%22%20-and%20%24UserInput%20-ne%20%22N%22)%0A%0A%20%20%20%20%20%20%20%20if(%24UserInput%20-eq%20%22Y%22)%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Canceling%20operation%22%0A%20%20%20%20%20%20%20%20%20%20%20%20Stop-AzSqlDatabaseActivity%20-ResourceGroupName%20%24ResourceGroupName%20-ServerName%20%24ServerName%20-DatabaseName%20%24DatabaseName%20-OperationId%20%24Operation.OperationId%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%20%0A%20%20%20%20%20%20%20%20%7B%22Exiting%20without%20cenceling%20the%20operation%22%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22No%20import%20or%20export%20operation%20is%20now%20running%22%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3CLI%20class%3D%22lia-indent-padding-left-30px%22%3Euse%20the%20function%26nbsp%3B%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ECancel-AzSQLImportExportOperation%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3Eto%20cancel%20an%20Import%20or%20Export%20operation%3C%2FLI%3E%0A%3CLI%20class%3D%22lia-indent-padding-left-30px%22%3Eyou%20need%20to%20provide%20the%20Resource%20Group%20name%2C%20Server%20name%20and%20Database%20name%20where%20the%20operation%20is%20currently%20running.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20you%20find%20it%20useful.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you%26nbsp%3BRoshna%20Nazir%20for%20pointing%20out%20this%20functionality.%3C%2FP%3E%0A%3CP%3Eif%20you%20have%20any%20feedback%20please%20do%20not%20hesitate%20to%20provide%20it%20in%20a%20comment%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYochanan.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Version history
Last update:
‎Nov 27 2020 04:02 AM
Updated by: