How to fix Error 10340 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 10340 – DROP ASSEMBLY Failed

Why This Error Happens

Error 10340 occurs when SQL Server’s CLR integration engine cannot remove an assembly because it is still referenced by dependent objects such as user-defined types, triggers, stored procedures, or functions. This can also happen if the assembly’s underlying file on disk is locked, corrupted, or if the internal metadata in the sys.assemblies catalog view has become inconsistent due to an interrupted transaction. Additionally, insufficient permissions (lacking CONTROL permission on the assembly or ALTER ANY ASSEMBLY at the database level) will trigger this specific HRESULT failure.

Step-by-Step Resolution Guide

Step 1: Identify Dependent Objects

  • Open SQL Server Management Studio (SSMS) and connect to the instance running on Windows Server 2025.
  • Run the following query to check for dependencies before attempting the drop again:
  SELECT referencing_major_id, referencing_minor_id 
  FROM sys.sql_expression_dependencies 
  WHERE referenced_id = OBJECT_ID('YourAssemblyName');
  • Alternatively, query sys.assembly_modules and sys.assembly_types to find linked CLR procedures or types that must be dropped first.

Step 2: Verify and Elevate Permissions

  • Ensure the executing login has the CONTROL permission on the specific assembly.
  • Run this T-SQL command to grant the necessary rights if missing:
  GRANT CONTROL ON ASSEMBLY::YourAssemblyName TO [YourUserName];
  • Check Windows Event Viewer (Applications and Services Logs > SQL Server) for any underlying Access Denied errors related to the SQL Server service account.

Step 3: Force Drop Dependent Objects

  • Manually script and drop all dependent CLR Stored Procedures, Functions, Triggers, or Aggregates identified in Step 1.
  • Execute the following sequence to clear the dependency chain:
  DROP PROCEDURE IF EXISTS dbo.YourCLRProcedure;
  DROP TYPE IF EXISTS dbo.YourCLRType;
  • Attempt the DROP ASSEMBLY YourAssemblyName; command again immediately after clearing dependencies.

Step 4: Restart the SQL Server Service

  • If the error persists due to a “ghost” lock on the metadata, restart the SQL Server service via the Services console (services.msc) on Windows Server 2025.
  • Alternatively, use PowerShell to restart the service cleanly:
  Restart-Service -Name 'MSSQLSERVER' -Force
  • This clears any cached execution contexts that might be holding a phantom reference to the assembly.

Alternative or Verification Steps

  • Verify the Fix: Run SELECT * FROM sys.assemblies WHERE name = 'YourAssemblyName'; after the drop command executes; if the query returns zero rows, the assembly has been successfully removed.
  • Alternative Solution: If the assembly remains “stuck” due to corruption, use ALTER DATABASE [YourDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; to force a clean state, execute the drop command, and then switch the database back to MULTI_USER mode.