Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Permanently delete one Gmail message

See more GMail REST API Examples

Permanent deletion. Supply the ID of a message you intend to erase. This removes it immediately and cannot be undone. Use Move one Gmail message to Trash for recoverable removal. Google requires the full mail scope for permanent deletion.

Authorize the application with https://mail.google.com/. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oToken
LOCAL oHttp
LOCAL oResp
LOCAL oReply
LOCAL oUrl

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

oToken := CreateObject("Chilkat.JsonObject")
nSuccess := oToken:LoadFile("gmail-token.json")
IF (nSuccess != 1)
    ? "Could not read gmail-token.json from the private working directory."
    oToken:destroy()
    RETURN
ENDIF

IF ((oToken:JsonTypeOf("access_token") != 1) .OR. (oToken:StringOfEquals("access_token", "", 1) == 1))
    ? "An OAuth access token is required."
    oToken:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := oToken:StringOf("access_token")
oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
oUrl:Append("GMAIL_MESSAGE_ID")
nSuccess := oHttp:HttpNoBody("DELETE", oUrl:GetAsString(), oResp)
IF (oResp:StatusCode == 0)
    ? "Gmail did not return an HTTP response."
    oToken:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    oUrl:destroy()
    RETURN
ENDIF

IF ((nSuccess != 1) .OR. (oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
    ? "Gmail HTTP " + Str(oResp:StatusCode)
    nSuccess := oReply:Load(oResp:BodyStr)
    IF (nSuccess == 1)
        ? oReply:StringOf("error.message")
    ENDIF

    oToken:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    oUrl:destroy()
    RETURN
ENDIF

//  Successful deletion has no message body to parse.
? "Message permanently deleted."

oToken:destroy()
oHttp:destroy()
oResp:destroy()
oReply:destroy()
oUrl:destroy()