Delete a file or folder with Graph API

There are several reasons why one might want to delete a SharePoint file or folder via the Graph API using Power Automate. Firstly, automating deletion can help maintain the organization and cleanliness of a SharePoint site, especially when dealing with temporary or obsolete files. Secondly, it allows for the enforcement of data retention policies by programmatically removing files that are past their retention period. Thirdly, it can be used to streamline workflows by deleting files that have been processed, reducing clutter and potential confusion.

To delete a file or folder from a SharePoint site using Microsoft Graph, you’ll need to follow a series of steps that involve obtaining various IDs and making the correct API calls. Here’s a detailed breakdown:

  1. Obtain the Site ID: https://learn.microsoft.com/en-us/graph/api/site-get The first step is to retrieve the Site ID for the SharePoint site from which you want to delete the file or folder. You can do this by making a GET request to https://graph.microsoft.com/v1.0/sites/{site-host-name}:/{site-path}. The response will contain the Site ID, which you’ll use in subsequent steps.

    An example uri might be https://graph.microsoft.com/v1.0/sites/mytenantname.sharepoint.com:/sites/DamienBird?$select=id,sharepointIds note that I have limited the data returned via the use of the select parameter.
  2. Obtain the Drive ID via Graph: https://learn.microsoft.com/en-us/graph/api/driveitem-get Once you have the Site ID, the next step is to get the Drive ID. This is done by making a GET request to https://graph.microsoft.com/v1.0/sites/{site-id}/drive. This endpoint will return information about the many document libraries associated with the site, including the Drive ID of each.

    An example uri might be https://graph.microsoft.com/v1.0/sites/7606d6a8-m685-6772-o9b9-256bi326r0d5/drives?$select=name,id,weburl note that you will have to filter the returned data based on the SharePoint Document Library name.
  3. Obtain the Item ID: Using the same driveItem api, and armoured with the Site ID and Drive ID, you can now obtain the Item ID of the file or folder you wish to delete. This requires a GET request to https://graph.microsoft.com/v1.0/sites/{site-id}/drives/{drive-id}/root:/{item-relative-path}. Replace {item-relative-path} with the relative path of the file or folder within the drive. The response will provide you with the Item ID.

    An example uri might be https://graph.microsoft.com/v1.0/sites/7606d6a8-m685-6772-o9b9-56bi326r0d5/drives/b!6KDAMOXGckSpuSVuwybgpddqV9bB2R1GgjWYDVSbY1jylqOkm2JlSL1
    POEcxHzgc/root:/my first level folder/my second level folder/my file.docx?$select=id,name,weburl
  4. Delete the File or Folder: https://learn.microsoft.com/en-us/graph/api/driveitem-delete Finally, to delete the item, you’ll make a DELETE request to https://graph.microsoft.com/v1.0/drives/{drive-id}/items/{item-id}. Ensure you have the necessary permissions to perform this action, as it will permanently delete the item from the drive.

    An example uri might be https://graph.microsoft.com/v1.0/drives/b!6KDAMOXGckSpuSVuwybgpddqV9bB2R1GgjWYDV
    SbY1jylqOkm2JlSL1POEcxHzgc/ items/01CHUSTNUOVXYBLLOGWRGKZKNZ25VGYDBE

Graph API 1.0 and SharePoint REST API 2.0 are interconnected through Microsoft Graph, which acts as a gateway to data within the Microsoft 365 platform. Essentially, Microsoft Graph API provides a unified endpoint, /v1.0, that you can use to access and manage resources across various Microsoft services, including SharePoint. The SharePoint REST API, on the other hand, is specifically designed to interact with SharePoint resources such as sites, lists, and libraries. By using Microsoft Graph, you can perform operations on SharePoint entities in a more streamlined way, without having to directly interact with the SharePoint REST API 2.0 endpoints.

The Power Automate Cloud Flow

  1. Connection to Graph API can be achieved as a delegated user with permissions to the SharePoint site via the Entra AD HTTP action where the base resource URL and Entra ID resource URI is set to http://graph.microsoft.com

2. Consider setting up four variables or key values in a parse JSON for your tenant, sitename, document library name and file or folder path.

3. Per step 1 above, Get the SiteID

4. Per step 2 above, Get the DriveID

5. In order to complete step 2, filter the response and retrieve the DriveID for the particular Document Library

6. Per step 3 above, get the ItemID

7. Per step 4 above, delete the file or folder

The completed Flow

SharePoint V2.0 and Graph v1.0 APIs are closely related

Did you know that instead of using Graph API, you can use the SharePoint REST API? The Microsoft Graph and SharePoint REST APIs are both interfaces that allow developers to interact with SharePoint data. The SharePoint REST API is a legacy interface that enables direct communication with SharePoint sites and content. On the other hand, the Microsoft Graph API is a newer, unified API that provides access to a wide range of Microsoft services, including SharePoint. Essentially, the Microsoft Graph acts as a gateway that includes SharePoint functionality, offering a more integrated experience across various Microsoft services. For instance, if you have a solution that already uses Microsoft Graph for accessing other Microsoft services, it’s recommended to use Microsoft Graph endpoints for SharePoint as well. This approach simplifies code management and ensures future compatibility, as Microsoft is moving towards centralizing its APIs through Microsoft Graph. However, if your application is solely focused on SharePoint, then using the SharePoint REST API might be sufficient. It’s important to note that while the Microsoft Graph API is becoming the preferred method for new developments, the SharePoint REST API still remains functional and is used for certain specific SharePoint operations that may not yet be available through Microsoft Graph. Operations using SharePoint REST v2 (Microsoft Graph) endpoints | Microsoft Learn

Above we can see how the SharePoint HTTP action can be used to delete the same file using the Drive and File ID obtained from earlier in the flow. In theory it is possible to run all of the above actions on the SharePoint REST API.

Share