delete()

Deletes one or more records from your organization’s data.

Syntax

DeleteResult[] = connection.delete(ID[] ids);

Usage

Use delete() to delete one or more existing records, such as individual accounts or contacts, in your organization’s data. The delete() call is analogous to the DELETE statement in SQL.

Rules and Guidelines

When deleting objects, consider the following rules and guidelines:

Rollback on Error

The AllOrNoneHeader header allows you to roll back all changes unless all records are processed successfully. This header is available in API version 20.0 and later. The default behavior is to allow partial success of a call: records without errors are committed, while records with errors are marked as failed in the call results.

Basic Steps for Deleting Records

Deleting records involves the following basic steps:

  1. Determine the ID of each record that you want to delete. For example, you might call query() to retrieve a set of records that you want to delete based on specific criteria.
  2. Construct an ID[] array and populate it with the IDs of each record that you want to delete. You can specify the IDs of different types of objects in the same call. For example, you could specify the ID for an individual Account and an individual Contact in the same array. For information on IDs, see ID Field Type.
  3. Call delete(), passing in the ID[] array.
  4. Process the results in the DeleteResult[] to verify whether the records have been successfully deleted.

Sample Code—Java

This sample shows how to delete records based on record IDs. The method in this sample accepts an array of IDs, which it passes to the delete() call and makes the call. It then parses the results and writes the IDs of the deleted records to the console or the first returned error if the deletion failed.

public void deleteRecords(String[] ids) {
   try {
      DeleteResult[] deleteResults = connection.delete(ids);
      for (int i = 0; i < deleteResults.length; i++) {
         DeleteResult deleteResult = deleteResults[i];
         if (deleteResult.isSuccess()) {
            System.out
                  .println("Deleted Record ID: " + deleteResult.getId());
         } else {
            // Handle the errors.
            // We just print the first error out for sample purposes.
            Error[] errors = deleteResult.getErrors();
            if (errors.length > 0) {
               System.out.println("Error: could not delete " + "Record ID "
                     + deleteResult.getId() + ".");
               System.out.println("   The error reported was: ("
                     + errors[0].getStatusCode() + ") "
                     + errors[0].getMessage() + "\n");
            }
         }
      }
   } catch (ConnectionException ce) {
      ce.printStackTrace();
   }
}

Sample Code—C#

This sample shows how to delete records based on record IDs. The method in this sample accepts an array of IDs, which it passes to the delete() call and makes the call. It then parses the results and writes the IDs of the deleted records to the console or the first returned error if the deletion failed.

public void deleteRecords(String[] ids)
{
   try
   {
      DeleteResult[] deleteResults = binding.delete(ids);
      for (int i = 0; i < deleteResults.Length; i++)
      {
         DeleteResult deleteResult = deleteResults[i];
         if (deleteResult.success)
         {
            Console.WriteLine("Deleted Record ID: " + deleteResult.id);
         }
         else
         {
            // Handle the errors.
            // We just print the first error out for sample purposes. Error[] errors = deleteResult.errors; if (errors.Length > 0) { Console.WriteLine("Error: could not delete " + "Record ID " + deleteResult.id + "."); Console.WriteLine(" The error reported was: (" + errors[0].statusCode + ") " + errors[0].message + "\n"); } } } } catch (SoapException e) { Console.WriteLine("An unexpected error has occurred: " + e.Message + "\n" + e.StackTrace); } }

Arguments

Name Type Description
ids ID[] Array of one or more IDs associated with the objects to delete. In version 7.0 and later, you can pass a maximum of 200 object IDs to the delete() call. In version 6.0 and earlier, the limit is 2,000.

Response

DeleteResult[]

Faults

InvalidSObjectFault

UnexpectedErrorFault

© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.