Sitecore Experience Commerce – XC9 – The Uninstall Script

Sitecore Experience Commerce is the new offering from Sitecore in the commerce arena and it is getting lot of attraction. This blog presents a PowerShell script to help out developers during the uninstall process.

I have posted a reference installation script that I have used during installation, (originally taken from the documentation and then added actual version for the packages) so you can always refer to it and compare it with your own script.

It may happen that during your installation, you have entered an incorrect value for a parameter or path to one of the packages. Then you may want to uninstall the the module before you can re-install it again.  Or may be you just want to uninstall the XC9 module to start from scratch. The process of uninstalling XC9 module can be difficult to do manually, so I present a a simple PowerShell script to automate it!

All you need to do is update the Param values (between line 3 and 15) to suit your installation variables and then run from the PS console:

#define parameters
Param(
[string]$Prefix = 'sitecore901',
[string]$CommerceOpsSiteName = 'CommerceOps_Sc9',
[string]$CommerceShopsSiteName = 'CommerceShops_Sc9',
[string]$CommerceAuthoringSiteName = 'CommerceAuthoring_Sc9',
[string]$CommerceMinionsSiteName = 'CommerceMinions_Sc9',
[string]$SitecoreBizFxSiteName = 'SitecoreBizFx',
[string]$SitecoreIdentityServerSiteName = 'SitecoreIdentityServer',
[string]$SolrService = 'Solr-6.6.2',
[string]$PathToSolr = 'C:\Solr\Solr-6.6.2',
[string]$SqlServer = 'DESKTOP\SQLEXPRESS',
[string]$SqlAccount = 'sa',
[string]$SqlPassword = 'mysupersecretpassword'
)
#Write-TaskHeader function modified from SIF
Function Write-TaskHeader {
param(
[Parameter(Mandatory=$true)]
[string]$TaskName,
[Parameter(Mandatory=$true)]
[string]$TaskType
)
function StringFormat {
param(
[int]$length,
[string]$value,
[string]$prefix = '',
[string]$postfix = '',
[switch]$padright
)
# wraps string in spaces so we reduce length by two
$length = $length 2 #– $postfix.Length – $prefix.Length
if($value.Length -gt $length){
# Reduce to length – 4 for elipsis
$value = $value.Substring(0, $length 4) + ''
}
$value = " $value "
if($padright){
$value = $value.PadRight($length, '*')
} else {
$value = $value.PadLeft($length, '*')
}
return $prefix + $value + $postfix
}
$actualWidth = (Get-Host).UI.RawUI.BufferSize.Width
$width = $actualWidth ($actualWidth % 2)
$half = $width / 2
$leftString = StringFormat length $half value $TaskName prefix '[' postfix ':'
$rightString = StringFormat length $half value $TaskType postfix ']' padright
$message = ($leftString + $rightString)
Write-Host ''
Write-Host $message ForegroundColor 'Red'
}
Function Remove-Service{
[CmdletBinding()]
param(
[string]$serviceName
)
if(Get-Service "My Service" ErrorAction SilentlyContinue){
sc.exe delete $serviceName
}
}
Function Remove-Website{
[CmdletBinding()]
param(
[string]$siteName
)
$appCmd = "C:\windows\system32\inetsrv\appcmd.exe"
& $appCmd delete site $siteName
}
Function Remove-AppPool{
[CmdletBinding()]
param(
[string]$appPoolName
)
$appCmd = "C:\windows\system32\inetsrv\appcmd.exe"
& $appCmd delete apppool $appPoolName
}
#Stop Solr Service
Write-TaskHeader TaskName "Solr Services" TaskType "Stop"
Write-Host "Stopping solr service"
Stop-Service $SolrService Force ErrorAction stop
Write-Host "Solr service stopped successfully"
#Delete solr cores
Write-TaskHeader TaskName "Solr Services" TaskType "Delete Cores"
Write-Host "Deleting Solr Cores"
$pathToCores = "$pathToSolr\server\solr\$Prefix*"
Remove-Item $pathToCores recurse force ErrorAction stop
Write-Host "Solr Cores deleted successfully"
#Remove Sites and App Pools from IIS
Write-TaskHeader TaskName "Internet Information Services" TaskType "Remove Websites"
Write-Host "Deleting Website $CommerceOpsSiteName"
Remove-Website siteName $CommerceOpsSiteName ErrorAction stop
Write-Host "Websites deleted"
Write-Host "Deleting Website $CommerceShopsSiteName"
Remove-Website siteName $CommerceShopsSiteName ErrorAction stop
Write-Host "Websites deleted"
Write-Host "Deleting Website $CommerceAuthoringSiteName"
Remove-Website siteName $CommerceAuthoringSiteName ErrorAction stop
Write-Host "Websites deleted"
Write-Host "Deleting Website $CommerceMinionsSiteName "
Remove-Website siteName $CommerceMinionsSiteName ErrorAction stop
Write-Host "Websites deleted"
Write-Host "Deleting Website $SitecoreBizFxSiteName"
Remove-Website siteName $SitecoreBizFxSiteName ErrorAction stop
Write-Host "Websites deleted"
Write-Host "Deleting Website $SitecoreIdentityServerSiteName"
Remove-Website siteName $SitecoreIdentityServerSiteName ErrorAction stop
Write-Host "Websites deleted"
Remove-AppPool appPoolName $CommerceOpsSiteName ErrorAction stop
Write-Host "Application pools deleted"
Remove-AppPool appPoolName $CommerceShopsSiteName ErrorAction stop
Write-Host "Application pools deleted"
Remove-AppPool appPoolName $CommerceAuthoringSiteName ErrorAction stop
Write-Host "Application pools deleted"
Remove-AppPool appPoolName $CommerceMinionsSiteName ErrorAction stop
Write-Host "Application pools deleted"
Remove-AppPool appPoolName $SitecoreBizFxSiteName ErrorAction stop
Write-Host "Application pools deleted"
Remove-AppPool appPoolName $SitecoreIdentityServerSiteName ErrorAction stop
Remove-Item C:\inetpub\wwwroot\$CommerceOpsSiteName recurse force ErrorAction stop
Write-Host "Websites removed from wwwroot"
Remove-Item C:\inetpub\wwwroot\$CommerceShopsSiteName recurse force ErrorAction stop
Write-Host "Websites removed from wwwroot"
Remove-Item C:\inetpub\wwwroot\$CommerceAuthoringSiteName recurse force ErrorAction stop
Write-Host "Websites removed from wwwroot"
Remove-Item C:\inetpub\wwwroot\$CommerceMinionsSiteName recurse force ErrorAction stop
Write-Host "Websites removed from wwwroot"
Remove-Item C:\inetpub\wwwroot\$SitecoreBizFxSiteName recurse force ErrorAction stop
Write-Host "Websites removed from wwwroot"
Remove-Item C:\inetpub\wwwroot\$SitecoreIdentityServerSiteName recurse force ErrorAction stop
Write-Host "Websites removed from wwwroot"
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 database SitecoreCommerce9_Global")
$commerceDbPrefix = $("DROP DATABASE IF EXISTS [SitecoreCommerce9_Global]")
Write-Host $("Query: $($commerceDbPrefix)")
invoke-sqlcmd ServerInstance $SqlServer U $SqlAccount P $SqlPassword Query $commerceDbPrefix ErrorAction stop
Write-Host $("Dropping database [SitecoreCommerce9_SharedEnvironments]")
$sharedDbPrefix = $("DROP DATABASE IF EXISTS [SitecoreCommerce9_SharedEnvironments]")
Write-Host $("Query: $($sharedDbPrefix)")
invoke-sqlcmd ServerInstance $SqlServer U $SqlAccount P $SqlPassword Query $sharedDbPrefix ErrorAction stop
Write-Host "Databases dropped successfully"
pop-location

Copy it locally in the deploy folder and run it like .\uninstall-commerce.ps1

All the script is doing is un-installing the SolrIndexes, IIS websites, website physical files and databases.

If you want to completely uninstall Sitecore 9 update 1 version, then use this script

Enjoy!

Naveed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.