Remove app from SharePoint Online using PowerShell

This SharePoint PowerShell tutorial, we will discuss, how to remove an app from SharePoint Online site using PowerShell. Here I have a SharePoint online developer site that has few SharePoint hosted add-ins deployed to the site.

If you are new to SharePoint hosted add-in previously known as SharePoint hosted apps, then you can see a video tutorial, Create and Deploy SharePoint Hosted Add-in using Visual Studio.

Remove SharePoint online Add-in or Apps using Browser

We can easily remove SharePoint add-in or apps using a browser. Open the site and then go to the “Apps in Testing” library (I am doing this in a developer site). You can read about an article on the Developer site template overview and also you can read how to create a developer site in Office 365 SharePoint online.

Then click on (…) on the particular Add-in you want to remove. Then click on Remove like below.

remove app from sharepoint online
remove app from sharepoint online

This will remove the add-in from the site.

Remove app from SharePoint Online using PowerShell

But sometimes while removing an add-in may be from the visual studio or manually, it does not remove properly. Sometimes it gave an error like: sorry we couldn’t remove the app.

In these cases, the best way is to remove the SharePoint hosted add-in by using PowerShell. But we need to first retrieve the AppInstanceId.

How to retrieve AppInstanceId of Add-in or APP in SharePoint online?

Open the site and then go to the “Apps in Testing” library. And then click on the (…) and then click on VIEW LOGS like below:

remove app sharepoint online powershell
remove app sharepoint online powershell

Then from browser, you can see the AppInstanceId like below:

sharepoint online remove app using PowerShell
sharepoint online remove app using PowerShell

Once the AppInstanceId is ready now we can run the below PowerShell script to remove the Add-in.

PowerShell script to remove SharePoint Add-in

You can run, debug and test the below PowerShell script in a Windows PowerShell ISE or inside a visual studio code. Make sure to give the below dll references:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
Add-Type -Path "E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$AppInstanceid = New-Object Guid("6F91CD30-7B5D-4A08-A06E-A72A82051813")
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/DevSite/"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "**********" -AsPlainText -Force
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials(“bijay@onlysharepoint2013.onmicrosoft.com”, $securePassword)
$web = $ctx.Web
$appInst = $ctx.Web.GetAppInstanceById($AppInstanceid)
$appInst.Uninstall();
$ctx.ExecuteQuery()

Once you run the above PowerShell script, you can see the removing status, here you can see it is showing Uninstalling.

sharepoint online remove app or add-in using Powershell
sharepoint online remove app or add-in using Powershell

After some time if you will refresh, the SharePoint add-in will already be removed from the SharePoint site.

Read some PowerShell Tutorials:

In this SharePoint tutorial, we discussed how to remove app from SharePoint Online using PowerShell.

>