Remove SharePoint document sharing using SharePoint REST API

SharePoint offers the ability to share files to specific users. This feature is different from directly settings permissions for the item. This article explains how to remove the sharings using SharePoint REST API.

Sometimes you might want to remove all file sharings for a document but not reset the items permission. For our test usecase an item exists that has broken permissions:

Now we create an anonymous link for this item. Afterwards the item permission look like this:

Now we want to remove this Sharing permission using SharePoint REST API but leave the other unique permissions intact.

First you need to get the sharing information of your document:

POST {SharePointWebUrl}/_api/web/lists(guid'{ListId}')/items({itemId})/GetSharingInformation?$select=permissionsInformation

This returns the following data:

{
    "@odata.context": "https://MyTestTenant.sharepoint.com/sites/SharingTest/_api/$metadata#SharingInformations/$entity",
    "@odata.type": "#SP.Sharing.SharingInformation",
    "@odata.id": "https://MyTestTenant.sharepoint.com/sites/SharingTest/_api/web/Lists(guid'38bf6425-e974-4e8c-9033-3aaa1e379275')/Items(2)/GetSharingInformation",
    "@odata.editLink": "web/Lists(guid'38bf6425-e974-4e8c-9033-3aaa1e379275')/Items(2)/GetSharingInformation",
    "permissionsInformation": {
      "hasInheritedLinks": false,
      "links": [
        {
          "isInherited": false,
          "linkDetails": {
            "AllowsAnonymousAccess": true,
            "ApplicationId": null,
            "BlocksDownload": false,
            "Created": "2022-02-21T13:33:09.918Z",
            "CreatedBy": {
              ...
            },
            "Description": null,
            "Embeddable": false,
            "Expiration": "",
            "HasExternalGuestInvitees": false,
            "Invitations": [],
            "IsActive": true,
            "IsAddressBarLink": false,
            "IsCreateOnlyLink": false,
            "IsDefault": true,
            "IsEditLink": true,
            "IsFormsLink": false,
            "IsManageListLink": false,
            "IsReviewLink": false,
            "IsUnhealthy": false,
            "LastModified": "2022-02-21T13:33:09.918Z",
            "LastModifiedBy": {
              ...
            },
            "LimitUseToApplication": false,
            "LinkKind": 5,
            "PasswordLastModified": "",
            "PasswordLastModifiedBy": null,
            "RedeemedUsers": [],
            "RequiresPassword": false,
            "RestrictedShareMembership": false,
            "Scope": 0,
            "ShareId": "bd826f05-832c-4f8b-84b8-5844c4b3a50e",
            "ShareTokenString": "share=EUS0lPErO09GvLGCFlzVz_MBFbMl5FL-Iu33F4HCq4EzVA",
            "SharingLinkStatus": 0,
            "TrackLinkUsers": false,
            "Url": "https://..."
          },
          "linkMembers": []
        },
        {
          "isInherited": false,
          "linkDetails": {
            "AllowsAnonymousAccess": false,
            "ApplicationId": null,
            "BlocksDownload": false,
            "Created": "",
            "CreatedBy": null,
            "Description": null,
            "Embeddable": false,
            "Expiration": "",
            "HasExternalGuestInvitees": false,
            "Invitations": [],
            "IsActive": false,
            "IsAddressBarLink": false,
            "IsCreateOnlyLink": false,
            "IsDefault": true,
            "IsEditLink": false,
            "IsFormsLink": false,
            "IsManageListLink": false,
            "IsReviewLink": false,
            "IsUnhealthy": false,
            "LastModified": "",
            "LastModifiedBy": null,
            "LimitUseToApplication": false,
            "LinkKind": 4,
            "PasswordLastModified": "",
            "PasswordLastModifiedBy": null,
            "RedeemedUsers": [],
            "RequiresPassword": false,
            "RestrictedShareMembership": false,
            "Scope": -1,
            "ShareId": "00000000-0000-0000-0000-000000000000",
            "ShareTokenString": null,
            "SharingLinkStatus": 0,
            "TrackLinkUsers": false,
            "Url": null
          },
          "linkMembers": []
        }
      ],
      ...
      ],
      ...
    }
  }

Afterwards you need to get all Links from JSON.permissionsInformation.links where SharedId != 00000000-0000-0000-0000-000000000000 (because all items with empty guid 00000000-0000-0000-0000-000000000000 are just normal item permission and no sharings)

For each of those items call the following endpoint:

POST {SharePointWebUrl}/_api/web/lists(guid'{ListId}')/items({itemId})/UnshareLink

with Data:

{"shareId":"{Link.ShareId}"}

Afterwards the sharing has been removed but the item special permissions look like this: