@@ -505,7 +505,38 @@ def install_from_marketplace_pkg(tenant_id: str, plugin_unique_identifiers: Sequ
505505
506506 @staticmethod
507507 def uninstall (tenant_id : str , plugin_installation_id : str ) -> bool :
508+ from extensions .ext_database import db
509+ from models .provider import ProviderCredential
510+ from sqlalchemy import select
511+
508512 manager = PluginInstaller ()
513+
514+ # Get plugin info before uninstalling to delete associated credentials
515+ try :
516+ plugins = manager .list_plugins (tenant_id )
517+ plugin = next ((p for p in plugins if p .installation_id == plugin_installation_id ), None )
518+
519+ if plugin :
520+ plugin_id = plugin .plugin_id
521+ logger .info (f"Deleting credentials for plugin: { plugin_id } " )
522+
523+ # Delete provider credentials that match this plugin
524+ credentials = db .session .scalars (
525+ select (ProviderCredential ).where (
526+ ProviderCredential .tenant_id == tenant_id ,
527+ ProviderCredential .provider_name .like (f"{ plugin_id } /%" ),
528+ )
529+ ).all ()
530+
531+ for cred in credentials :
532+ db .session .delete (cred )
533+
534+ db .session .commit ()
535+ logger .info (f"Deleted { len (credentials )} credentials for plugin: { plugin_id } " )
536+ except Exception as e :
537+ logger .warning (f"Failed to delete credentials: { e } " )
538+ # Continue with uninstall even if credential deletion fails
539+
509540 return manager .uninstall (tenant_id , plugin_installation_id )
510541
511542 @staticmethod
0 commit comments