Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix: clear stale provider credentials during plugin uninstall (#32380)
-LAN-fbacb9f7a279
diff --git a/api/services/plugin/plugin_service.py b/api/services/plugin/plugin_service.pyindex 411c335c175..6eed3a6b386 100644--- a/api/services/plugin/plugin_service.py+++ b/api/services/plugin/plugin_service.py@@ -3,13 +3,15 @@ from collections.abc import Mapping, Sequencefrom mimetypes import guess_typefrom pydantic import BaseModel-from sqlalchemy import select+from sqlalchemy import delete, select, update+from sqlalchemy.orm import Sessionfrom yarl import URLfrom configs import dify_configfrom core.helper import marketplacefrom core.helper.download import download_with_size_limitfrom core.helper.marketplace import download_plugin_pkg+from core.helper.model_provider_cache import ProviderCredentialsCache, ProviderCredentialsCacheTypefrom core.plugin.entities.bundle import PluginBundleDependencyfrom core.plugin.entities.plugin import (PluginDeclaration,@@ -28,7 +30,7 @@ from core.plugin.impl.debugging import PluginDebuggingClientfrom core.plugin.impl.plugin import PluginInstallerfrom extensions.ext_database import dbfrom extensions.ext_redis import redis_client-from models.provider import ProviderCredential+from models.provider import Provider, ProviderCredentialfrom models.provider_ids import GenericProviderIDfrom services.errors.plugin import PluginInstallationForbiddenErrorfrom services.feature_service import FeatureService, PluginInstallationScope@@ -511,30 +513,55 @@ class PluginService:manager = PluginInstaller()# Get plugin info before uninstalling to delete associated credentials- try:- plugins = manager.list_plugins(tenant_id)- plugin = next((p for p in plugins if p.installation_id == plugin_installation_id), None)-- if plugin:- plugin_id = plugin.plugin_id- logger.info("Deleting credentials for plugin: %s", plugin_id)-- # Delete provider credentials that match this plugin- credentials = db.session.scalars(- select(ProviderCredential).where(- ProviderCredential.tenant_id == tenant_id,- ProviderCredential.provider_name.like(f"{plugin_id}/%"),- )- ).all()+ plugins = manager.list_plugins(tenant_id)+ plugin = next((p for p in plugins if p.installation_id == plugin_installation_id), None)++ if not plugin:+ return manager.uninstall(tenant_id, plugin_installation_id)- for cred in credentials:- db.session.delete(cred)+ with Session(db.engine) as session, session.begin():+ plugin_id = plugin.plugin_id+ logger.info("Deleting credentials for plugin: %s", plugin_id)- db.session.commit()- logger.info("Deleted %d credentials for plugin: %s", len(credentials), plugin_id)- except Exception as e:- logger.warning("Failed to delete credentials: %s", e)- # Continue with uninstall even if credential deletion fails+ # Delete provider credentials that match this plugin+ credential_ids = session.scalars(+ select(ProviderCredential.id).where(+ ProviderCredential.tenant_id == tenant_id,+ ProviderCredential.provider_name.like(f"{plugin_id}/%"),+ )+ ).all()++ if not credential_ids:+ logger.info("No credentials found for plugin: %s", plugin_id)+ return manager.uninstall(tenant_id, plugin_installation_id)++ provider_ids = session.scalars(+ select(Provider.id).where(+ Provider.tenant_id == tenant_id,+ Provider.provider_name.like(f"{plugin_id}/%"),+ Provider.credential_id.in_(credential_ids),+ )+ ).all()++ session.execute(update(Provider).where(Provider.id.in_(provider_ids)).values(credential_id=None))++ for provider_id in provider_ids:+ ProviderCredentialsCache(+ tenant_id=tenant_id,+ identity_id=provider_id,+ cache_type=ProviderCredentialsCacheType.PROVIDER,+ ).delete()++ session.execute(+ delete(ProviderCredential).where(+ ProviderCredential.id.in_(credential_ids),+ )+ )++ logger.info(+ "Completed deleting credentials and cleaning provider associations for plugin: %s",+ plugin_id,+ )return manager.uninstall(tenant_id, plugin_installation_id)