Sample code for 30+ languages & platforms
Xbase++

Delete All Files

See more Google Drive Examples

Permanently deletes all files owned by the user without moving it to the trash.

This example works by first getting a list of all fileIds, and then iterating over the list to delete each file.

See Google Drive Files delete for more information.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oGAuth
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL oJson
LOCAL i
LOCAL nNumFiles
LOCAL cJsonResponse
LOCAL nPageNumber
LOCAL cPageToken
LOCAL nBContinueLoop
LOCAL cFileId
LOCAL oSaFileIds
LOCAL nBHasMorePages
LOCAL oSbPath

nSuccess := 0

nSuccess := 1

//  It requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  This example uses a previously obtained access token having permission for the 
//  Google Drive scope.

oGAuth := CreateObject("Chilkat.AuthGoogle")
oGAuth:AccessToken := "GOOGLE-DRIVE-ACCESS-TOKEN"

oRest := CreateObject("Chilkat.Rest")

//  Connect using TLS.
nBAutoReconnect := 1
nSuccess := oRest:Connect("www.googleapis.com", 443, 1, nBAutoReconnect)

//  Provide the authentication credentials (i.e. the access token)
oRest:SetAuthGoogle(oGAuth)

//  Get 10 results per page for testing.  (The default page size is 100, with a max of 1000.
oRest:AddQueryParam("pageSize", "10")

oJson := CreateObject("Chilkat.JsonObject")

//  Send the request for the 1st page.
cJsonResponse := oRest:FullRequestNoBody("GET", "/drive/v3/files")

nPageNumber := 1

nBContinueLoop := oRest:LastMethodSuccess .AND. (oRest:ResponseStatusCode == 200)

oSaFileIds := CreateObject("Chilkat.StringArray")

DO WHILE nBContinueLoop == 1

    ? "---- Page " + Str(nPageNumber) + " ----"
    oJson:Load(cJsonResponse)

    nNumFiles := oJson:SizeOfArray("files")
    i := 0
    DO WHILE i < nNumFiles
        oJson:I := i
        cFileId := oJson:StringOf("files[i].id")
        ? "name: " + oJson:StringOf("files[i].name")
        ? "id: " + cFileId
        oSaFileIds:Append(cFileId)
        i := i + 1
    ENDDO

    //  Get the next page of files.
    //  If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
    //  for the next request.   If no "nextPageToken" was present, then this was the last page of files.
    cPageToken := oJson:StringOf("nextPageToken")
    nBContinueLoop := 0
    nBHasMorePages := oJson:LastMethodSuccess
    IF (nBHasMorePages == 1)
        oRest:ClearAllQueryParams()
        oRest:AddQueryParam("pageSize", "10")
        oRest:AddQueryParam("pageToken", cPageToken)
        cJsonResponse := oRest:FullRequestNoBody("GET", "/drive/v3/files")
        nBContinueLoop := oRest:LastMethodSuccess .AND. (oRest:ResponseStatusCode == 200)
        nPageNumber := nPageNumber + 1
    ENDIF

ENDDO

//  Before actually deleting, check for errors...
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oGAuth:destroy()
    oRest:destroy()
    oJson:destroy()
    oSaFileIds:destroy()
    RETURN
ENDIF

//  A successful response will have a status code equal to 200.
IF (oRest:ResponseStatusCode != 200)
    ? "response status code = " + Str(oRest:ResponseStatusCode)
    ? "response status text = " + oRest:ResponseStatusText
    ? "response header: " + oRest:ResponseHeader
    ? "response JSON: " + cJsonResponse
    oGAuth:destroy()
    oRest:destroy()
    oJson:destroy()
    oSaFileIds:destroy()
    RETURN
ENDIF

//  OK, we have the full list of files.  Delete each..
oSbPath := CreateObject("Chilkat.StringBuilder")
nNumFiles := oSaFileIds:Count
i := 0
DO WHILE i < nNumFiles
    cFileId := oSaFileIds:GetString(i)

    oRest:ClearAllQueryParams()

    oSbPath:Clear()
    oSbPath:Append("/drive/v3/files/")
    oSbPath:Append(cFileId)

    cJsonResponse := oRest:FullRequestNoBody("DELETE", oSbPath:GetAsString())
    IF (oRest:LastMethodSuccess != 1)
        ? oRest:LastErrorText
        oGAuth:destroy()
        oRest:destroy()
        oJson:destroy()
        oSaFileIds:destroy()
        oSbPath:destroy()
        RETURN
    ENDIF

    //  A successful response will have a status code equal to 204 and the response body is empty.
    //  (If not successful, then there should be a JSON response body with information..)
    IF (oRest:ResponseStatusCode != 204)
        ? "response status code = " + Str(oRest:ResponseStatusCode)
        ? "response status text = " + oRest:ResponseStatusText
        ? "response header: " + oRest:ResponseHeader
        ? "response JSON: " + cJsonResponse
        oGAuth:destroy()
        oRest:destroy()
        oJson:destroy()
        oSaFileIds:destroy()
        oSbPath:destroy()
        RETURN
    ENDIF

    i := i + 1
    ? Str(i) + ": Deleted " + cFileId
ENDDO

? "All Files Deleted."

oGAuth:destroy()
oRest:destroy()
oJson:destroy()
oSaFileIds:destroy()
oSbPath:destroy()