Users.messages: delete

Requires authorization

Immediately and permanently deletes the specified message. This operation cannot be undone. Prefer messages.trash instead. Try it now or see an example.

Request

HTTP request

DELETE https://www.googleapis.com/gmail/v1/users/userId/messages/id

Parameters

Parameter name Value Description
Path parameters
id string The ID of the message to delete.
userId string The user's email address. The special value me can be used to indicate the authenticated user.

Authorization

This request requires authorization with the following scope (read more about authentication and authorization).

Scope
https://mail.google.com/

Request body

Do not supply a request body with this method.

Response

If successful, this method returns an empty response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

import com.google.api.services.gmail.Gmail;

import java.io.IOException;

// ...

public class MyClass {

  // ...


  /**
   * Immediately and permanently deletes the specified thread. This operation cannot
   * be undone. Prefer threads.trash instead.
   *
   * @param service Authorized Gmail API instance.
   * @param userId User's email address. The special value "me"
   * can be used to indicate the authenticated user.
   * @param threadId ID of Thread to delete.
   * @throws IOException
   */
  public static void deleteThread(Gmail service, String userId, String threadId)
      throws IOException {
    service.users().threads().delete(userId, threadId).execute();
    System.out.println("Thread with id: " + threadId + " deleted successfully.");
  }

  // ...
}

.NET

Uses the .NET client library.

using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// Delete a Message.
  /// </summary>
  /// <param name="service">Gmail API service instance.</param>
  /// <param name="userId">User's email address. The special value "me"
  /// can be used to indicate the authenticated user.</param>
  /// <param name="messageId">ID of the Message to delete.</param>
  public static void DeleteMessage(GmailService service, String userId, String messageId)
  {
      try
      {
          service.Users.Messages.Delete(userId, messageId).Execute();
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Delete Message with given ID.
 *
 * @param  Google_Service_Gmail $service Authorized Gmail API instance.
 * @param  string $userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  string $messageId ID of Message to delete.
 */
function deleteMessage($service, $userId, $messageId) {
  try {
    $service->users_messages->delete($userId, $messageId);
    print 'Message with ID: ' . $messageId . ' successfully deleted.';
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }
}

Python

Uses the Python client library.

"""Delete a Message.
"""

from apiclient import errors


def DeleteMessage(service, user_id, msg_id):
  """Delete a Message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    msg_id: ID of Message to delete.
  """
  try:
    service.users().messages().delete(userId=user_id, id=msg_id).execute()
    print 'Message with id: %s deleted successfully.' % msg_id
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

JavaScript

Does not use a client library.

/**
 * Delete Message with given ID.
 *
 * @param  {String} userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  {String} messageId ID of Message to delete.
 */
function deleteMessage(userId, messageId) {
  var request = gapi.client.gmail.users.messages.delete({
    'userId': userId,
    'id': messageId
  });
  request.execute(
    function(resp) { });
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response.

Enviar comentarios sobre…

¿Necesitas ayuda? Visita nuestra página de asistencia.