# delete()

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

## Syntax

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

## Usage

Use [`delete()`](/docs/platform/api/guide/sforce-api-calls-delete.md) to delete one or more existing records, such as individual accounts or contacts, in your organization's data. The [`delete()`](/docs/platform/api/guide/sforce-api-calls-delete.md) call is analogous to the `DELETE` statement in SQL.

## Rules and Guidelines

When deleting objects, consider these rules and guidelines.

- Your client application must be logged in with sufficient access rights to delete individual objects within the specified object. For more information, see [Factors that Affect Data Access](/docs/platform/api/guide/calls.md).
- In addition, you could also need permission to access this object's parent object. For special access requirements, see the object's description in [Standard Objects](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_list.htm).
- To ensure referential integrity, the [`delete()`](/docs/platform/api/guide/sforce-api-calls-delete.md) call supports cascading deletions. If you delete a parent object, you delete its children automatically, as long as each child object can be deleted. For example, if you delete a [Case](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_case.htm), the API automatically deletes any [CaseComment](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_casecomment.htm), CaseHistory, and [CaseSolution](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_casesolution.htm) objects associated with that case. However, if a [CaseComment](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_casecomment.htm) is not deletable or is being used, then the [`delete()`](/docs/platform/api/guide/sforce-api-calls-delete.md) call on the parent [Case](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_case.htm) fails.
- Certain objects cannot be deleted via the API. To delete an object via the [`delete()`](/docs/platform/api/guide/sforce-api-calls-delete.md) call, its object must be configured as deletable (`deletable` is `true`) . To determine whether a given object can be deleted, your client application can invoke the [`describeSObjects()`](/docs/platform/api/guide/sforce-api-calls-describesobjects.md) call on the object and inspect its `deletable` property.
- You can't delete records for multiple object types in one call if one of those types is related to a feature in the Setup area in Salesforce. The only exceptions are these objects.

  - Custom settings objects, which are similar to custom objects. For more information, see Create Custom Settings in Salesforce Help .
  - GroupMember
  - Group
  - User

## Rollback on Error

The [AllOrNoneHeader](/docs/platform/api/guide/sforce-api-header-allornoneheader.md) 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. Lets a call roll back all changes unless all records are processed successfully.

## Basic Steps for Deleting Records

Deleting records involves these basic steps.

1. Determine the ID of each record that you want to delete. For example, you call [`query()`](/docs/platform/api/guide/sforce-api-calls-query.md) 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](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_account.htm) and an individual [Contact](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_contact.htm) in the same array. For information on IDs, see [ID Field Type](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/field_types.htm#i1435616).
3. Call [`delete()`](/docs/platform/api/guide/sforce-api-calls-delete.md), 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.

```codesegment
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.

```codesegment
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](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/field_types.htm#i1435330)[] | 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()`](/docs/platform/api/guide/sforce-api-calls-delete.md) call. In version 6.0 and earlier, the limit is 2,000. |

## Response

[DeleteResult](/docs/platform/api/guide/sforce-api-calls-delete-result.md)\[]

## Faults

[`InvalidSObjectFault`](/docs/platform/api/guide/sforce-api-calls-concepts-core-data-objects.md)

[`UnexpectedErrorFault`](/docs/platform/api/guide/sforce-api-calls-concepts-core-data-objects.md)

**See Also**

- [API Call Basics](/docs/platform/api/guide/calls.md)
