How to uninstall a Sitecore package in seconds

Installing a package is always easy in Sitecore but package uninstall has always been a grey area. There are a couple of options like these: aspx-based or then, Sitecore Rocks Anti-package.

I then thought why not some PS scripting. So, here it is:



Pre-requisites: Package file name must match name as what is present in <webroot>\App_Data\packages

Copy over the package zip file as it is from <webroot>\App_Data\packages folder  and place with uninstall package script in same folder and execute script through PowerShell command prompt. 

The script, unzips the package file, finds the location of all files and deletes those in the target instance and then finally deletes the package file from <webroot>\App_Data\packages folder too.


Just a few seconds to do the job!

Random check:

After the run, Services.GraphQL folder not present in the instance folder system:



Showconfig.aspx:

Cons: Can't handle Sitecore content packages





#https://navansitecorenotes.blogspot.com/2023/06/powershell-script-that-uninstalls.html
##############################################
[CmdletBinding()]
param(
# Web Root folder
[Parameter(Mandatory = $true)]
[string]
[ValidateNotNullOrEmpty()]
$SitePhysicalRoot = "C:\inetpub\wwwroot\sc103lkgsc.dev.local",
# Zip file by default to be present in current execution folder
[string]
[ValidateNotNullOrEmpty()]
$PackageFileLocation = "."
)
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$watch.Start() # Timer start
$ErrorActionPreference = "Stop";
$FileNameWithFullPath = (Get-Item ".\*.zip").FullName
$PackageFileName = Split-Path $FileNameWithFullPath -Leaf
Write-Host $SitePhysicalRoot"\App_Data\packages\"$PackageFileName
if (-not (Test-Path -Path $SitePhysicalRoot"\App_Data\packages\"$PackageFileName -PathType Leaf)) {
Write-Host "No Package named " $PackageFileName " installed in the instance"
exit
}
# Unzip the package zip
Expand-Archive -Path "*.zip" -Force
Write-Host "Analysing Package!"
#Write-Host $FileNameWithFullPath
$PackageFileLocation = Split-Path $FileNameWithFullPath -Parent
#Write-Host "Package file unzip path " + $PackageFileLocation
$FileNameWithoutExt = (Get-Item ".\*.zip").BaseName
Write-Host "Package file whole path " $PackageFileLocation\$FileNameWithoutExt
Expand-Archive -Path $PackageFileLocation\$FileNameWithoutExt\package.zip -DestinationPath $PackageFileLocation\$FileNameWithoutExt -Force
$SourcePath = $PackageFileLocation + "\" + $FileNameWithoutExt + "\files"
Write-Host "Source path " $SourcePath
Write-Host "Destination path " $SitePhysicalRoot
$files = Get-ChildItem -Path $SourcePath -Recurse -Force -File
Write-Host "Starting uninstall Operation at " $SitePhysicalRoot
foreach ($file in $files)
{
$fileName = $file.FullName.Replace($SourcePath,$SitePhysicalRoot)
Write-Host "Removing " $fileName
if (Test-Path $fileName) {
Remove-Item $fileName -Force
}
}
Write-Host "Clearing blank folders at " $SitePhysicalRoot
$Directories = Get-ChildItem $SitePhysicalRoot -Directory -Recurse | Where-Object { (Get-ChildItem $_.FullName).count -eq 0 } | Select-Object -ExpandProperty FullName
$Directories | ForEach-Object { Remove-Item $_ }
Write-Host "Removing package zip from " $SitePhysicalRoot
Remove-Item $SitePhysicalRoot"\App_Data\packages\"$PackageFileName -Force
Write-Host "Package uninstall process complete!"
$watch.Stop() # Stopping the timer
Write-Host "Execution time in seconds " $watch.Elapsed # Print script execution time
##############################################

Comments