Sample code for 30+ languages & platforms
PureBasic

Delete File

See more Google Drive Examples

Permanently deletes a file owned by the user without moving it to the trash. If the target is a folder, all descendants owned by the user are also deleted.

See Google Drive Files delete for more information.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthGoogle.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    success = 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.

    gAuth.i = CkAuthGoogle::ckCreate()
    If gAuth.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthGoogle::setCkAccessToken(gAuth, "GOOGLE-DRIVE-ACCESS-TOKEN")

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect using TLS.
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"www.googleapis.com",443,1,bAutoReconnect)

    ; Provide the authentication credentials (i.e. the access token)
    CkRest::ckSetAuthGoogle(rest,gAuth)

    ; To delete a file, we must use the fileId.
    ; This must've been obtained by listing or searching for the file
    ; to get the metadata.
    ; Assume we already did that an have the fileId
    fileId.s = "0B53Q6OSTWYoldUprZVU1ZVQ5Z0k"

    sbPath.i = CkStringBuilder::ckCreate()
    If sbPath.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbPath,"/drive/v3/files/")
    CkStringBuilder::ckAppend(sbPath,fileId)

    jsonResponse.s = CkRest::ckFullRequestNoBody(rest,"DELETE",CkStringBuilder::ckGetAsString(sbPath))
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkAuthGoogle::ckDispose(gAuth)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbPath)
        ProcedureReturn
    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 CkRest::ckResponseStatusCode(rest) <> 204
        Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "response status text = " + CkRest::ckResponseStatusText(rest)
        Debug "response header: " + CkRest::ckResponseHeader(rest)
        Debug "response JSON: " + jsonResponse
        CkAuthGoogle::ckDispose(gAuth)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbPath)
        ProcedureReturn
    EndIf

    Debug "File deleted."


    CkAuthGoogle::ckDispose(gAuth)
    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbPath)


    ProcedureReturn
EndProcedure