Decor

How to Detect and Remove Duplicate SharePoint Items Using SQL Server and Power Automate

Problem

During long-running SharePoint migrations, delta migrations are often required to keep SharePoint Online synchronized with the On-Premises environment.

Incorrect migration mappings or missing unique identifiers may cause existing records to be copied instead of updated. This is especially common in lists where multiple items share the same business identifier.

As a result, duplicate records may appear with different SharePoint Item IDs.

Duplicate SharePoint list records with different Item IDs

Solution: SQL Server and Power Automate

The approach is straightforward: identify duplicate records using SQL Server, export their business identifiers to CSV, and remove the affected items using Power Automate.

Step 1. Access SharePoint Data in SQL Server

First, SharePoint list data must be made available in SQL Server.

One possible approach is to extract the required fields using PnP PowerShell and load them into a SQL staging table.

For this example, we'll use YourList with the fields ID, RecordKey, AuthorName, Created, and Modified.

Step 2. Find Duplicate Records

The following SQL query identifies records with matching business attributes:

SELECT
    RecordKey,
    AuthorName,
    Created,
    Modified,
    COUNT(*) AS DuplicateCount
FROM dbo.YourList
GROUP BY
    RecordKey,
    AuthorName,
    Created,
    Modified
HAVING COUNT(*) > 1
ORDER BY RecordKey;

Alternatively, EXISTS can identify matching records with different SharePoint Item IDs:

SELECT DISTINCT
    t1.RecordKey,
    t1.AuthorName
FROM dbo.YourList AS t1
WHERE EXISTS (
    SELECT 1
    FROM dbo.YourList AS t2
    WHERE t2.RecordKey = t1.RecordKey
      AND t2.AuthorName = t1.AuthorName
      AND t2.Created = t1.Created
      AND t2.Modified = t1.Modified
      AND t2.ID <> t1.ID
);

The comparison fields should be adjusted to match each list's business logic. Multiple records sharing the same RecordKey are not necessarily duplicates.

SQL query results showing duplicate record groups

Step 3. Export Records for Cleanup

Once duplicates are identified, retrieve the affected business identifiers:

SELECT DISTINCT RecordKey
FROM (
    SELECT RecordKey, AuthorName, Created, Modified
    FROM dbo.YourList
    GROUP BY RecordKey, AuthorName, Created, Modified
    HAVING COUNT(*) > 1
) AS Duplicates;

Copy the results, including the RecordKey column header, and save them as a CSV file.

RecordKey values exported for CSV cleanup file

Step 4. Remove Records with Power Automate

Create an instant cloud flow using Manually trigger a flow and add a File input to upload the CSV.

The flow reads the CSV and processes each RecordKey using Apply to each.

For every identifier, the SharePoint Get items action retrieves matching records using an OData filter. A second loop processes the returned items and passes their SharePoint IDs to Delete item.

Power Automate flow deleting duplicate SharePoint items

Important: This approach removes all matching records, including the original item. It should only be used when the records can be restored from a trusted source.

Step 5. Restore and Validate

After cleanup, the affected records can be migrated again from SharePoint On-Premises using the corrected ShareGate mapping.

Make sure the migration job supports restoring deleted destination items, as a standard delta migration may not automatically recreate them.

Once the records have been restored, refresh the SQL data and run the validation query:

SELECT
    RecordKey,
    AuthorName,
    Created,
    Modified,
    COUNT(*) AS DuplicateCount
FROM dbo.YourList
GROUP BY
    RecordKey,
    AuthorName,
    Created,
    Modified
HAVING COUNT(*) > 1
ORDER BY RecordKey;

If the query returns no unexpected duplicate groups, the cleanup has been successful according to the defined comparison criteria.

Validation query returning no duplicate groups

Conclusion

Combining SQL Server and Power Automate provides a practical way to identify and clean up duplicate SharePoint records during complex migrations.

SQL handles duplicate detection, while Power Automate automates cleanup, reducing manual effort and helping maintain data consistency.