Delete an Azure SQL Database from PowerShell

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

One of the things I’ve been working on is trying to get my DevOps, continuous delivery pipeline working with Azure. Part of that is a test deployment to an Azure SQL Database, which means I need to be able to update an existing database from a backup. Unfortunately, Azure SQL Database doesn’t support a restore over an existing database (yet).

That means one task I have is to remove an existing database, in order to replace it with a new database with the same name. A little work in the PoSh documentation found Remove-AzureRMSQLDatabase, which is just what I need.

To use this cmdlet, I need a connection to the Azure space first. I can do that with a credential that I get with this command. Ultimately I need to store this, but interactively this lets me get started:

Login-AzureRMAccount

This gets me an interactive login. I enter my account and password. Since I use a Live account, this won’t work in the pipeline, but it gets me going.

2017-03-10 09_00_34-Sign in to your account

From here, I can set a few variables I’ll need. I want the name of a resource group, a server, and a database. In my case, I’ll use a few variables. I call the database the “new” one, since I’ll be using an existing one that I’ll recreate from an “old” one.

2017-03-10 09_03_07-powershell - How to Login without prompt_ - Stack Overflow

From here, it’s just a question of calling the Remove-AzureRmSqlDatabase cmdlet with parameters. I do that, and get results. Here’s the call

Remove-AzureRmSqlDatabase -ServerName $server -ResourceGroupName $rgname -DatabaseName $newname

Here are the results. I’ve blacked out a few ids.

2017-03-10 09_05_16-Photos

This clears the database, and after refreshing, I can see it’s gone from my list of Azure SQL Databases.

2017-03-10 09_08_12-SQL databases - Microsoft Azure

Not much to this, but it’s part of a larger scheme, which is getting a copy of the production database and restoring it.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , , . Bookmark the permalink.