"Violation of PRIMARY KEY constraint 'Perms_PK'. Cannot insert duplicate key.


  • Dear All,

    Currently I'm facing the following error when I'm trying to delete the orphans objects using the following command by Powershell:

    Command used:

    Get-SPContentDatabase | ForEach-Object { $$true) | out-file -filePath (“d:\temp\” + $ + “.log”)}


    Exception calling "Repair" with "1" argument(s): "Violation of PRIMARY KEY constraint 'Perms_PK'. Cannot insert duplicate key in
    object 'dbo.Perms'. The duplicate key value is (2ecffbc8-bd8e-4c01-81fc-d2975c50045f, 0x, sites/4tpjr660ie0a/SharedDocuments/7.0_
    The statement has been terminated."
    At line:1 char:52
    +  Get-SPContentDatabase | ForEach-Object { $ <<<< ($true) | out-file -filePath (“d:\temp\” + $ + “.log”)}
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    How should I delete the Orphan objects that I have?

    SharePoint 2010 version > 14.0.6131.5003

    Friday, September 20, 2013 11:00 AM

All replies

  • Hi Gonzalo,

    According to your description, my understanding is that you want to delete the Orphan objects in your environment. Please  run following script in power shell:

    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )

     {Add-PSSnapin Microsoft.SharePoint.Powershell}

    set-location "C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\BIN"

    $SPFarm = Get-SPFarm

    $contentWebAppServices = (Get-SPFarm).services | ? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

    foreach ($SPWebApp in $contentWebAppServices.WebApplications)


    foreach ($SPSite in $SPWebApp.Sites)


    Write-Host "Going thru Site Collection" $SPSite.Url

    $SPSite.Url >> C:\SPSiteOrphanObject.txt

    stsadm.exe -o databaserepair -url $SPSite.Url -databasename $SPSite.ContentDatabase.Name >> C:\SPSiteOrphanObject.txt



    For more information, please refer to the blog:

    For the error you encountered, it can be caused by the Orphan object which is created without any permissions. You can refer to the article for more information:

    Please inform me freely if you have any questions.


    Monday, September 23, 2013 11:30 AM
  • Thank you,

    I ran the script that you sent me but just list the Orphan Objects that I have. I think the issue is related with the Blog that you mention for permissions:

    I was looking and in the script to ResetRoleInheritance but is just mentioned with List, currently this is one of my orphan items.

    <OrphanedObjects Count="1">
      <Orphan Type="SecurityScope" SiteId="{40C25286-E509-43E7-8ADE-0D3CAD31130D}" Name="sites/4tp3rtnusbc7/SharedDocuments/Test.docx" InRecycleBin="No" />

    I has been checking and I find the following data to run the script:

    $site = Get-SPSite

    $web = Get-SPWeb

    But then I will need the following data:

    @OldScopeId = ‘B2D7B4C0-8E02-4E5F-A611-020BE5770A8F’,
    @Url = N’web/list/16855_.000′,
    @DocId = ‘fab7dc68-50ad-4ea0-9c99-230dfdbc0567′,

    How can I take the Doc ID? in order to use the Store proceadure?

    USE [WSS_Content]


    GODECLARE @return_value int,
    @NewScopeId uniqueidentifier,
    @RequestGuid uniqueidentifier

    EXEC @return_value = [dbo].[proc_SecResetItemPerm]
    @SiteId = ’9f07b6ef-25e1-4a7a-b06e-f60019e20255′,
    @WebId = ‘c3536be5-a419-4c27-88fd-269316c18757′,
    @OldScopeId = ‘B2D7B4C0-8E02-4E5F-A611-020BE5770A8F’,
    @Url = N’web/list/16855_.000′,
    @DocId = ‘fab7dc68-50ad-4ea0-9c99-230dfdbc0567′,
    @NewScopeId = @NewScopeId OUTPUT,
    @RequestGuid = @RequestGuid OUTPUT

    SELECT @NewScopeId as N’@NewScopeId’,
    @RequestGuid as N’@RequestGuid’

    SELECT ‘Return Value’ = @return_value

    Thanks in advance,

    Monday, September 23, 2013 3:20 PM
  • Hi Gonzalo,

    According your description, please run the following command based on the list of the orphan objects:

    stsadm.exe -o databaserepair -url SPSite.Url -databasename SPSite.ContentDatabase.Name -deletecorruption

    Try to run the command and tell me the result.

    Please inform me freely if you have any questions.

    Best Regards


    Tuesday, September 24, 2013 1:48 AM
  • Hi Eric,

    As you can see in the script that I ran, the command above $SPSite.Dispose() was already there when I ran the script:

    stsadm.exe -o databaserepair -url $SPSite.Url -databasename $SPSite.ContentDatabase.Name >> C:\SPSiteOrphanObject.txt



    Can I do other update to script for testing?

    Tuesday, September 24, 2013 2:18 AM
  • Hi Gonzalo,

    To delete orphan objects, you can run the command based on the list of the Orphan Objects:

    stsadm.exe -o databaserepair -url SPSite.Url -databasename SPSite.ContentDatabase.Name -deletecorruption

    Best Regards


    Tuesday, September 24, 2013 2:27 AM