Search code examples
azurerestsharepointmicrosoft-graph-apisharepoint-online

Microsoft Graph API - How to Delete a Folder with Children


I could use some help deleting a folder using the Microsoft Graph API. The api call below works, but only when the folder is empty and has no children.

DELETE https://graph.microsoft.com/v1.0/drives/{{driveId}}/items/{{driveItemId}}
  1. The folders are created and deleted using the same client application and permissions
  2. It's a folder in a Sharepoint document library if that matters at all
  3. I tried adding an If-Match header with the current eTag of the folder, but that didn't make any difference.

Here's the response I'm getting:

409 Conflict
{
    "error": {
        "code": "resourceModified",
        "message": "The resource has changed since the caller last read it; usually an eTag mismatch",
        "innerError": {
            "date": "2022-10-17T21:40:30",
            "request-id": "b3d35695-...",
            "client-request-id": "b3d35695-..."
        }
    }
}

Is there a way to force the delete, or do I have to clear it out before I can delete it? If I have to clear it out, what's the easiest way to do that?


Solution

  • I tried to reproduce the same in my environment and got below results:

    I have one Azure AD application and granted API permissions like below:

    enter image description here

    To call Microsoft Graph, I acquired access token using client credentials flow via Postman as below:

    enter image description here

    I created folder in my SharePoint document library using above token with below query:

    POST https://graph.microsoft.com/v1.0/drives/<driveID>/items/
    {
    "name": "Sri Folder",
    "folder": { },
    }
    

    Response:

    enter image description here

    When I checked in SharePoint Online, folder created successfully in document library like below:

    enter image description here

    Now I added children to the created folder by uploading one file to it with below query:

    PUT https://graph.microsoft.com/v1.0/drives/<driveID>/items/<driveItemId>:/test.txt:/content
    

    Response:

    enter image description here

    When I checked the same in SharePoint Online, file is uploaded to the folder successfully like below:

    enter image description here

    Now, I used the same query as you to delete the folder with children by adding If-match header and got same error like below:

    DELETE https://graph.microsoft.com/v1.0/drives/<driveId>/items/<driveItemId>
    If-match : etag_value
    

    Response

    enter image description here

    To resolve the error, you need to replace If-match header with eTag.

    I tried the same query by replacing If-match header with eTag, it returned Status: 204 No Content like below:

    Response:

    enter image description here

    When I refreshed the page in SharePoint Online, got this error as folder got deleted along with its children:

    enter image description here

    I checked opening document library and cannot find the folder as it got deleted successfully:

    enter image description here