Delete multiple Azure SQL's with PowerShell

Nibbler 656 Reputation points
2021-04-18T17:27:37.133+00:00

Hi all

I have this (below) basic script to delete a single SQL database. But, would really like to be able to delete all SQL's where "Backup" is included in the name...so, a single script to delete them all.

Example:
Backup-SQL-01-01012021
Backup-SQL-02-01012021
Backup-SQL-03-01012021
Backup-SQL-04-01012021
Backup-SQL-05-01012021

The number of SQL's could be +100.

Script to delete SQL
Remove-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "BackupServer" -DatabaseName "Backup-SQL-01-01012021" -AsJob -Force

Ang great ideas to do this, and if it's possible?

Azure SQL Database
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-18T20:11:23.55+00:00

    Hi @KE1980 ,

    I just did some testing in my environment. Both scripts are working here now.
    In the first script I have to remove the quotes around the $_.DatabaseName

    Get-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "backupserver" -DatabaseName "Backup*" |
        ForEach {
         $_.DatabaseName # | Select *
        Remove-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "backupserver" -DatabaseName $_.DatabaseName -Force
    }
    

    And the second script is working from the beginning:

    Get-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "backupserver" -DatabaseName "Backup*" | 
            Remove-AzSqlDatabase -Force
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-18T17:52:46.983+00:00

    Hi @KE1980 ,

    you can give it a try with this. Run the script on your own risk. The script is not tested by myself!

    Get-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "BackupServer" -DatabaseName "Backup-SQL*" | 
    ForEach-Object {
        Remove-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "BackupServer" -DatabaseName "$_.DatabaseName" -AsJob -Force -WhatIf
        }
    

    The script above will not delete the databases as long as -Whatif is at the end of line 3. This way you can check what will happen ;-)
    If the script is working as required you can just remove the -Whatif in line 3


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-18T19:29:27.213+00:00

    Hi @KE1980 ,

    Could you please check the value of $_.DatabaseName in the `ForEach-Object" loop.
    It should be the name of the Database to delete.

    And just for testing you can try this:

    Get-AzSqlDatabase -ResourceGroupName "ResourceGroup-Backup" -ServerName "BackupServer" -DatabaseName "Backup*" | 
        Remove-AzSqlDatabase -WhatIf
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.