Home
%3CLINGO-SUB%20id%3D%22lingo-sub-825222%22%20slang%3D%22en-US%22%3EPreventing%20the%20disaster%20of%20accidental%20deletion%20for%20your%20MySQL%20database%20on%20Azure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-825222%22%20slang%3D%22en-US%22%3E%3CP%3EThere%E2%80%99s%20a%20special%20kind%20of%20sinking%20feeling%20in%20your%20stomach%20when%20you%20realize%20that%20you%20(or%20someone%20on%20your%20team)%20inadvertently%20deleted%20your%20database%2C%20especially%20your%20production%20database.%20You've%20been%20there%20before%3A%20you%20ran%20a%20command%20and%20as%20soon%20as%20you%20hit%20enter%20you%20realize%20what%20you've%20done.%20You%20cancel%20the%20command%20as%20fast%20as%20possible%2C%20but%20it's%20too%20late%2C%20the%20damage%20is%20done.%20Time%20starts%20moving%20in%20slow%20motion.%20Seconds%20seem%20like%20minutes%2C%20minutes%20like%20hours.%20How%20much%20data%20have%20you%20lost%2C%20can%20you%20even%20get%20things%20back%20up%2C%20are%20your%20backups%20gone%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20cloud%20has%20many%20benefits%2C%20one%20being%20that%20you%20can%20easily%20spin%20up%20a%20resource.%20But%20it%E2%80%99s%20true%20that%20you%20can%20just%20as%20easily%20spin%20one%20down.%20The%20good%20news%20is%20that%20you%20can%20prevent%20accidental%20deletion%20of%20your%20Azure%20Database%20for%20MySQL%20server%20(and%20other%20Azure%20resources)%20by%20using%20a%20*delete%20lock*.%20Delete%20locks%20are%20a%20type%20of%20management%20lock%20in%20Azure.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20use%20one%20command%20in%20the%20Azure%20CLI%20to%20create%20a%20delete%20lock%20on%20your%20MySQL%20server%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eaz%20lock%20create%20--lock-type%20CanNotDelete%20--resource-type%20Microsoft.DBforMySQL%2Fservers%20--name%20DeleteLockMySQL%20--resource-group%20samplegroup%20--resource-name%20thesampleserver%3C%2FCODE%3E%3C%2FPRE%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%0A%3CP%3ENow%20if%20someone%20tries%20to%20delete%20this%20server%20or%20its%20resource%20group%2C%20they%20will%20get%20an%20error%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EThis%20scope%20cannot%20perform%20delete%20operation%20because%20following%20scope(s)%20are%20locked%3A%20'%2Fsubscriptions%2F00000000000000000000000000%2Fresourcegroups%2Fsamplegroup%2Fproviders%2FMicrosoft.DBforMySQL%2Fservers%2Fthesampleserver'.%20Please%20remove%20the%20lock%20and%20try%20again.%3C%2FCODE%3E%3C%2FPRE%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%0A%3CP%3EYou%20can%20even%20put%20a%20delete%20lock%20at%20the%20resource%20group%20level.%20One%20advantage%20of%20doing%20this%20is%20that%20new%20resources%20are%20automatically%20protected%20by%20the%20delete%20lock.%20This%20is%20a%20great%20way%20of%20protecting%20production%20resources%20from%20accidental%20deletion.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20command%20locks%20a%20resource%20group%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eaz%20lock%20create%20--lock-type%20CanNotDelete%20--name%20DeleteLockGroup%20--resource-group%20samplegroup%3C%2FCODE%3E%3C%2FPRE%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%0A%3CP%3ENow%20if%20someone%20tries%20to%20delete%20the%20group%20or%20a%20resource%20in%20that%20group%2C%20they%20will%20get%20an%20error.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%3A%20To%20create%20or%20delete%20a%20lock%2C%20you%20must%20have%20access%20to%26nbsp%3BMicrosoft.Authorization%2F*or%20Microsoft.Authorization%2Flocks%2F*%20actions.%20Owner%20and%20User%20Access%20Administrator%20are%20the%20two%20built-in%20roles%20that%20are%20granted%20those%20actions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20also%20implement%20delete%20locks%20using%20the%20Azure%20portal%2C%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-mysql%2Ftree%2Fmaster%2Farm-templates%2FExampleWithLocks%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eas%20part%20of%20an%20ARM%20Template%3C%2FA%3E%2C%20PowerShell%20script%2C%20or%20REST%20API.%20Learn%20how%20to%20do%20so%20in%20the%20lock%20resources%20documentation.%20Integrating%20lock%20setup%20in%20your%20resource%20creation%20workflow%20will%20help%20safeguard%20against%20accidental%20disasters%20in%20the%20future.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-825222%22%20slang%3D%22en-US%22%3E%3CP%3EDon't%20be%20the%20person%20who%20accidentally%20drops%20production%20MySQL%20databases%20on%20Azure%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

There’s a special kind of sinking feeling in your stomach when you realize that you (or someone on your team) inadvertently deleted your database, especially your production database. You've been there before: you ran a command and as soon as you hit enter you realize what you've done. You cancel the command as fast as possible, but it's too late, the damage is done. Time starts moving in slow motion. Seconds seem like minutes, minutes like hours. How much data have you lost, can you even get things back up, are your backups gone?

 

The cloud has many benefits, one being that you can easily spin up a resource. But it’s true that you can just as easily spin one down. The good news is that you can prevent accidental deletion of your Azure Database for MySQL server (and other Azure resources) by using a *delete lock*. Delete locks are a type of management lock in Azure.

 

You can use one command in the Azure CLI to create a delete lock on your MySQL server:

 

 

az lock create --lock-type CanNotDelete --resource-type Microsoft.DBforMySQL/servers --name DeleteLockMySQL --resource-group samplegroup --resource-name thesampleserver

 

 

 

Now if someone tries to delete this server or its resource group, they will get an error:

 

 

This scope cannot perform delete operation because following scope(s) are locked: '/subscriptions/00000000000000000000000000/resourcegroups/samplegroup/providers/Microsoft.DBforMySQL/servers/thesampleserver'. Please remove the lock and try again.

 

 

 

You can even put a delete lock at the resource group level. One advantage of doing this is that new resources are automatically protected by the delete lock. This is a great way of protecting production resources from accidental deletion.

 

This command locks a resource group:

 

 

az lock create --lock-type CanNotDelete --name DeleteLockGroup --resource-group samplegroup

 

 

 

Now if someone tries to delete the group or a resource in that group, they will get an error.

 

Note: To create or delete a lock, you must have access to Microsoft.Authorization/* or Microsoft.Authorization/locks/* actions. Owner and User Access Administrator are the two built-in roles that are granted those actions.

 

You can also implement delete locks using the Azure portal, as part of an ARM Template, PowerShell script, or REST API. Learn how to do so in the lock resources documentation. Integrating lock setup in your resource creation workflow will help safeguard against accidental disasters in the future.