Can't delete sharepoint documents using results from graph search results

Stan Spotts 21 Reputation points
2022-09-21T18:45:40.263+00:00

I've got a search to find documents in SharePoint online document libraries not updated within a specific time frame. It's returning what I kind of expect.

POST https://graph.microsoft.com/v1.0/search/query  

Request Body

           {  
                "requests": [  
                    {  
                        "entityTypes": [  
                            "listItem"  
                        ],  
                        "query": {  
                            "queryString": "LastModifiedTime<2022-09-15 isDocument:true"  
                        },  
                        "fields": [  
                            "title",  
                            "UniqueId",  
                            "id",  
                            "ListID",  
                            "Filename",  
                            "IsDocument",  
                            "FileExtension",  
                            "WebUrl",  
                            "SiteID",  
                            "WebId",  
                            "Author",  
                            "LastModifiedTime"  
                        ]  
                    }  
                ]  
            }  

example of "resource" section of results:

{  
"@odata.type": "#microsoft.graph.listItem",  
"fields": {  
"title": "PowerPoint Template - Widescreen Size",  
"uniqueId": "{3BE7714A-EA2D-4B32-A66A-CFCE28C9zzzz}",  
"id": "AAAAAGaLPIjzUfFNrdwtmY6ZKr4HAGjxE0Vl4dxOpU2hoI6JGDgAAAAAAScAAGjxE0Vl4dxOpU2hoI6JGDgAAOWu4t0AAA2",  
"listID": "a3accbde-26ed-4dab-9e24-8f5548ec8462",  
"filename": "Info for Microsoft 365_Labelingupdate_050322.pptx",  
"isDocument": true,  
"fileExtension": "pptx",  
"siteID": "13c96220-50e5-4397-bfb9-cf72259fxxxx",  
"webId": "6e83fd9c-60db-40bc-82ff-11f1eb46yyyy",  
"author": "Stan Spotts",  
"lastModifiedTime": "2022-05-13T19:59:33Z"  
}  

The Graph API for delete doesn't find the file no matter what Id I try. For instance, this:

DELETE https://graph.microsoft.com/v1.0/sites/13c96220-50e5-4397-bfb9-cf72259fxxxx/drive/items/3BE7714A-EA2D-4B32-A66A-CFCE28C9zzzz

results in:

"code": "itemNotFound",
"message": "The resource could not be found."

Am I using the delete API incorrectly, or is it that the search API doesn't return the information I need for calling the delete API?

Please, no need to tell me about retention policies. This is a simplified search.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,833 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,862 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bhanu Kiran 3,526 Reputation points
    2022-09-21T21:13:36.173+00:00

    Hi @Stan Spotts ,

    Hope you are doing good.

    The item ID you are using to delete the item is incorrect, therefore you are getting an error message that states "itemNotFound".

    To retrieve the correct Item ID, you need to use the following endpoint:

    GET https://graph.microsoft.com/v1.0/sites/{site-ID}/drive/root/children  
    

    243593-itemid.png

    Alternatively, you can use "driveItem" as "entityTypes" to get the correct "ItemID"

    243625-hitid.png

    Using this ItemID you can delete the file :
    DELETE https://graph.microsoft.com/v1.0/sites/{site-ID}/drive/items/{itemID}

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


0 additional answers

Sort by: Most helpful