Remove Unique Permissions From SharePoint List And Library Using REST API

Introduction

In this article, we will see how to remove the unique permissions from the SharePoint list or library items.

Use Case

In my Documents library, I have a lot of files with unique permissions. I want to remove these unique permissions with the help of REST API. You can see that, I have added 3 files with unique permissions in the Documents library.

Script to remove unique permissions using REST API

We will call the function in the loop. Here variables names i represent the item ID. As we want to remove all the unique permissions from the library, we can start the ID from 1. In the below script, we are calling the myFunction in the loop.

We are also checking for the error code 400 in the error block, which represents that the item was deleted.

var i = 1;

function myFunction() {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Documents')/items(" + i + ")/ResetRoleInheritance()",
        type: 'POST',
        headers: {
            "Accept": "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "POST"
        },
        success: function(data) {
            console.log('item permissions removed successfully: ID = ' + i);
            i++;
            myFunction();
        },
        error: function(error) {
            console.log('failed: ID = ' + i);
            if (error.status = 400) {
                i++;
                myFunction();
            } else {
                alert(JSON.stringify(error));
            }
        }
    });
}
myFunction();

This will remove the unique permissions from all the files from the document library.