To Remove App Instance From App Catalog Using PowerShell Script

Scenario

Sometimes we add third party apps from Sharepoint Store to SPO app catalog site. However, these apps are not available for the users on SPO sites' Apps You Can Add section. The app was not properly installed because of the presence of stale entry; so we removed the app instances from app catalog and then added the apps again to appcatalog and now the apps are available for all SPO sites through addanapp.aspx page without navigating to SharePoint store.

1) To remove the app instance from app catalog, the account should have admin permission for app catalog site.

2) Now, we need to specify app Instance ID.

PowerShell script 

  1. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")  
  2. Function Get - ClientContext([string] $Url, [string] $UserName, [string] $Password) {  
  3.     $SecurePassword = $Password | ConvertTo - SecureString - AsPlainText - Force  
  4.     $context = New - Object Microsoft.SharePoint.Client.ClientContext($Url)  
  5.     $context.Credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)  
  6.     return $context  
  7. }  
  8. Function Uninstall - AppInstance([Microsoft.SharePoint.Client.ClientContext] $Context, [Guid] $AppInstanceId) {  
  9.     $appInst = $Context.Web.GetAppInstanceById($AppInstanceId)  
  10.     $appInst.Uninstall()  
  11.     $context.ExecuteQuery()  
  12. }  
  13. $UserName = "User ID"  
  14. $Password = Read - Host - AsSecureString "Enter the password"  
  15. $Url = "https://chrfr.sharepoint.com/sites/appcatalog"  
  16. $AppInstanceid = New - Object Guid("8d457aa3-89f6-45df-bf6a-b1845c00d3e9")# specify App Instance Id here  
  17. $creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)  
  18. $context = New - Object Microsoft.SharePoint.Client.ClientContext($Url)  
  19. $context.credentials = $creds  
  20. Uninstall - AppInstance - Context $context - AppInstanceId $AppInstanceid  
  21. $context.Dispose()   

Conclusion

In this blog, I provided a PowerShell script to remove the app instance from app catalog.