Search code examples
pythonpython-3.xmicrosoft-graph-apiazure-ad-msal

Python MSAL REST Graph: is it possible to get all files in folder, not just 200, in one request?


I need to delete all files from a OneDrive folder. When I issue a request like the one shown under # Listing children(all files and folders) within General here, I only get 200 items, which seems to be the expected default behavior per this. So in order to delete all files, I repeat the request multiple times, each time deleting 200 files. Is it possible to get ALL children in one request?


Solution

  • This worked:

    link = parent+":/children"
    while True:
        rGetCh = requests.get(link, headers=headers)
    
        for ch in rGetCh.json()["value"]:
            # Looping through the current list of children
            chName = urllib.parse.quote(ch["name"].encode('utf8'))
            chPath = parent + "/" + chName
            
        if "@odata.nextLink" in rGetCh.json():  # if this is in the response, there are more children
           link =  rGetCh.json()["@odata.nextLink"]
        else:
           break