In this blog, I will talk about removing the sandbox solution uploaded in Solution Gallery through CSOM Code. Suppose if you have a need to delete the sandbox solution if it already exists and add the updated WSP solution. Here is the code to achieve it.
// find the solution in the gallery and delete it
var solutionGallery = SiteContext.Web.Lists.GetByTitle(“Solution Gallery”);
var files = solutionGallery.RootFolder.Files;
//Load the entire solution Gallery.
SiteContext.Load(files, fs => fs.Where(f => f.Name == “MYWSP.wsp”));
var file = files.FirstOrDefault();
if (file != null)
Hope that helps! Happy SharePointing folks!!