0

Looking to see if this is possible. Our SQL server(s) have numerous databases from our customers where we are doing tech support for them. Once the support issues is done (3 months after resolved), we want to automatically delete the database (not just drop, but delete the actual files as well to reclaim disk space).

Our databases are all named in a particular format that matches the customers incident number. So we should be able to query the list of support issues resolved and compare that to the databases names attached to the SQL instance(s).

I am new to powershell, just wondering if such a thing would be possible as a script that could be run on a weekly basis.

Thank you all.

5
  • You may want to have a look at the dbatools PowerShell module Commented Jan 7, 2021 at 14:30
  • Creating a new database for each ticket feels like a design flaw. Commented Jan 7, 2021 at 14:30
  • One would imagine this is a copy of their customer's database as at the support ticket for investigation, which is even more of a design flaw. Commented Jan 7, 2021 at 14:32
  • Why don't you use SQLAgent which is dedicated to such a job ? And optimized for ??? Commented Jan 7, 2021 at 14:34
  • We keep separate databases as this is a massive application and knowing what happened at that exact time and what the data looked like is important. Customers tend delete transactions and try again which removes the affected data in some cases. Commented Jan 7, 2021 at 16:04

1 Answer 1

3

Yes, you can do this with PowerShell. With invoke-sqlcommand you can run any query you like, including dropping databases. So something like this:

Invoke-SQLCommand -ServerInstance MyServer\Instance -database master -query "DROP DATABASE [MyUnsupportedDatabase]"

Under normal circumstances, the database files will automatically be removed.

Needless to say, before you start deleting databases with a script, you will want to test this thoroughly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, that will get me going to go back to my team to tell them it can be done (politics...lol).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.