---
title: Remove duplicate EndPoints and related metrics from the Nodinite database
description: SQL script and guidance to safely remove duplicate rows from dbo.EndPoints and any associated metrics, log points, and metric values in a Nodinite configuration database.
---

# <i class="fal fa-database" aria-hidden="true"></i> Remove duplicate EndPoints and related metrics

Each <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Endpoint][Endpoints] should exist once per unique combination of type, [Log Agent][Log Agents] source, name, direction, and URI. Duplicate rows in the [Configuration Database][] register the same Endpoint more than once, each with its own statistics. This troubleshooting note shows how to identify and remove duplicate rows in the `dbo.EndPoints` table and delete any associated `LogPoints`, `MetricsInLogPoints`, and `MetricValues` that reference those duplicate endpoints.

Use this script only after taking a full backup of the database and preferably in a maintenance window or on a staging copy. The script deletes rows and cannot be undone without a backup.

## <i class="fal fa-clipboard-check" aria-hidden="true"></i> Prerequisites

* Verify you have a recent database backup.  
* Ensure you have appropriate SQL Server administrative privileges to DELETE rows and create temporary tables.  
* Prefer running the script on a non-production copy first to validate behavior.  

## <i class="fal fa-gears" aria-hidden="true"></i> What the Script Does

* Builds a temporary list of duplicate `EndPoints` (keeps the lowest `EndPointId` per unique key).  
* Iterates over each duplicate `EndPointId` and deletes related `MetricValues`, `MetricsInLogPoints`, `LogPoints`, then removes the `EndPoints` row.  
* Prints progress and skips any endpoint that raises an error during deletion.  
* At the end, reports any remaining duplicate groups and associated `EndPointId`s for manual inspection.  

## <i class="fal fa-terminal" aria-hidden="true"></i> Run the Script

Run the following T-SQL from a query window connected to the <span class="nodinite-product-name" title="The node that unites different systems. Or as we call it Nodinite">Nodinite</span> [Configuration Database][] (for example, `Nodinite_Prod_Config`):

```sql
IF OBJECT_ID(N'dbo.EndPoints', N'U') IS NULL
BEGIN
    RAISERROR(N'Table dbo.EndPoints does not exist in this database.', 16, 1);
    RETURN;
END;

IF OBJECT_ID(N'tempdb..#EndPointDuplicates', N'U') IS NOT NULL
    DROP TABLE #EndPointDuplicates;

SELECT
    e.EndPointId,
    e.EndPointTypeId,
    e.LogAgentSourceId,
    e.Name,
    e.Direction,
    e.URI
INTO #EndPointDuplicates
FROM (
    SELECT *,
        ROW_NUMBER() OVER (
            PARTITION BY EndPointTypeId, LogAgentSourceId, Name, Direction, URI
            ORDER BY EndPointId
        ) AS rn
    FROM dbo.EndPoints
) e
WHERE e.rn > 1;

DECLARE @CurrentEndPointId INT;

WHILE EXISTS (SELECT 1 FROM #EndPointDuplicates)
BEGIN
    SELECT TOP (1) @CurrentEndPointId = EndPointId
    FROM #EndPointDuplicates
    ORDER BY EndPointId;

    BEGIN TRY
        DELETE MV
        FROM dbo.MetricValues MV
        JOIN dbo.MetricsInLogPoints MILP ON MV.MetricId = MILP.MetricId
        JOIN dbo.LogPoints LP ON MILP.LogPointId = LP.LogPointId
        WHERE LP.EndPointId = @CurrentEndPointId;

        DELETE MILP
        FROM dbo.MetricsInLogPoints MILP
        JOIN dbo.LogPoints LP ON MILP.LogPointId = LP.LogPointId
        WHERE LP.EndPointId = @CurrentEndPointId;

        DELETE LP
        FROM dbo.LogPoints LP
        WHERE LP.EndPointId = @CurrentEndPointId;

        DELETE FROM dbo.EndPoints
        WHERE EndPointId = @CurrentEndPointId;

        PRINT CONCAT('Deleted duplicate endpoint Id=', @CurrentEndPointId);
    END TRY
    BEGIN CATCH
        PRINT CONCAT('Skipping EndPointId=', @CurrentEndPointId, ': ', ERROR_MESSAGE());
    END CATCH

    DELETE FROM #EndPointDuplicates
    WHERE EndPointId = @CurrentEndPointId;
END

;WITH RemainingDuplicates AS (
    SELECT
        EndPointTypeId,
        LogAgentSourceId,
        Name,
        Direction,
        URI,
        COUNT(*) AS TotalRows
    FROM dbo.EndPoints
    GROUP BY EndPointTypeId, LogAgentSourceId, Name, Direction, URI
    HAVING COUNT(*) > 1
)
SELECT
    rd.EndPointTypeId,
    rd.LogAgentSourceId,
    rd.Name,
    rd.Direction,
    rd.URI,
    rd.TotalRows,
    e.EndPointId
FROM RemainingDuplicates rd
INNER JOIN dbo.EndPoints e
    ON e.EndPointTypeId = rd.EndPointTypeId
    AND (
        (e.LogAgentSourceId IS NULL AND rd.LogAgentSourceId IS NULL)
        OR (e.LogAgentSourceId = rd.LogAgentSourceId)
    )
    AND e.Name = rd.Name
    AND e.Direction = rd.Direction
    AND e.URI = rd.URI
ORDER BY rd.TotalRows DESC, rd.EndPointTypeId, e.EndPointId;
```

## <i class="fal fa-triangle-exclamation" aria-hidden="true"></i> Notes and Cautions

* This script permanently deletes rows. Always have a verified backup before running.  
* The deletion order removes metric values and log points first to avoid orphaned foreign-key references.  
* If your database has custom constraints or triggers, review and test carefully — triggers may block or alter deletes.  
* If the script skips a row due to an error, record the printed message and investigate the cause (foreign-key violations, permission errors, or locked resources).  

If you prefer to only identify duplicates without deleting them, run the `SELECT` portion that populates `#EndPointDuplicates` and inspect the results before proceeding.

## <i class="fal fa-hand-point-right" aria-hidden="true"></i> Next Steps

* <i class="fal fa-trash-alt" aria-hidden="true"></i> [Manually removing old configuration data][]  
* <i class="fal fa-sign-out" aria-hidden="true"></i> [Endpoints][]  

## <i class="fal fa-link-horizontal" aria-hidden="true"></i> Related Topics

* <i class="fal fa-database" aria-hidden="true"></i> [SQL Server Troubleshooting][SQL Server Overview]  
* <i class="fal fa-database" aria-hidden="true"></i> [Configuration Database][]  
* <i class="fal fa-database" aria-hidden="true"></i> [Agent Database][]  
* <i class="fal fa-tools" aria-hidden="true"></i> [Manually update databases][]  
* <i class="fal fa-exchange-alt" aria-hidden="true"></i> [Database Migration Overview][]  

[Agent Database]:https://docs.nodinite.com/install-and-update/troubleshooting/sql-server/agent-database/
[Configuration Database]:https://docs.nodinite.com/core-services/configuration-database/overview/
[Database Migration Overview]:https://docs.nodinite.com/install-and-update/troubleshooting/sql-server/database-migration/overview/
[Endpoints]:https://docs.nodinite.com/repository-model/endpoints/overview/
[Log Agents]:https://docs.nodinite.com/administration/log-management/log-agents/features/what-is-a-log-agent/
[Manually removing old configuration data]:https://docs.nodinite.com/install-and-update/troubleshooting/sql-server/manually-removing-old-configuration-data/
[Manually update databases]:https://docs.nodinite.com/install-and-update/troubleshooting/sql-server/manually-update-databases/
[SQL Server Overview]:https://docs.nodinite.com/install-and-update/troubleshooting/sql-server/overview/