So, as a part of PowerShell commands, we have UnInstall-SPSolution command for retracting a SharePoint solution and Remove-SPSolution command to remove the solution from any scope.
When you execute these commands in a sequence in a script (.ps1) file, Usually we end up with an error saying
Remove-SPSolution : The solution "XXXX.wsp" has been deployed in the farm. Please retract the deployment before removing the solution.You can also use the
-override parameter to forcibly remove the solution, but you will not be able to retract the solution deployment.