PS Script to drop Sitecore and Commerce DBs


While https://navansitecorenotes.blogspot.com/2020/05/sitecore-and-sitecore-commerce-92.html is used to uninstall both Sitecore and Commerce instances, this PS script is used to just drop all the DBs because its a pain deleting old DBs one after another in SSMS.

Copy to a PS script file from below this line:
Param(
[string]$SiteName = "sc92commdev2.local",
        [string]$SqlServer = 'mysqlservername',
[string]$SqlAccount = 'sa',
[string]$SqlPassword = 'mysqlpwd'
)

#Write-TaskHeader -TaskName "SQL Server" -TaskType "Drop Databases"
#Drop databases from SQL
Write-Host "Dropping databases from SQL server"
push-location
import-module sqlps

Write-Host $("Dropping Sitecore databases")
$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Core]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Master]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Web]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_EXM.Master]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_ReferenceData]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Reporting]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_ExperienceForms]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_MarketingAutomation]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Processing.Pools]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Processing.Tasks]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_ProcessingEngineStorage]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_ProcessingEngineTasks]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Xdb.Collection.Shard0]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Xdb.Collection.Shard1]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Xdb.Collection.ShardMapManager]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$sitecoreDbPrefix = "DROP DATABASE IF EXISTS [" + $SiteName + "_Messaging]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $sitecoreDbPrefix -ErrorAction stop

$commerceDbPrefix = "DROP DATABASE IF EXISTS [" + $Prefix + "_Global]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $commerceDbPrefix -ErrorAction stop

$commerceDbPrefix = "DROP DATABASE IF EXISTS [" + $Prefix + "_SharedEnvironments]"
invoke-sqlcmd -ServerInstance $SqlServer -U $SqlAccount -P $SqlPassword -Query $commerceDbPrefix -ErrorAction stop

Write-Host "Databases dropped successfully"
pop-location

Comments