Newer Version Available
Delete Field History and Field Audit Trail Data
To delete field history and audit trail data, the user permissions Delete From Field History and Delete From Field History Archive must be enabled through a permission set or a user profile. The org preferences to enable these permissions, Delete From Field History and Delete From Field History Archive, are located in Setup | User Interface.
Delete field history data, such as AccountHistory, and field history archive data by passing in a list of ID values as strings using the Apex or SOAP delete() method. The Apex delete() method also works with a list of sObjects with the Id field populated.
The sObject acts like a template. All rows that match the sObject’s fields and values are deleted. You can only specify fields that are part of the big object’s index. You must specify all fields in the index. You can’t include a partially specified index or non-indexed field, and wildcards are not supported.
1List<AccountHistory> ah = new List<sObject>();
2ah.addAll( [ SELECT Id FROM AccountHistory
3WHERE AccountId = '001d000000Ky3xIAB' and CreatedDate = YESTERDAY ] );
4Database.delete(ah);Samples for deleting from FieldHistoryArchive:
1List<FieldHistoryArchive> fha = new List<sObject>();
2fha.addAll([SELECT FieldHistoryType, ParentId, CreatedDate, HistoryId FROM FieldHistoryArchive
3WHERE FieldHistoryType = 'Account' AND ParentId = '001d000000Ky3xIAB' AND CreatedDate = '2017-11-28T19:13:36.000z' AND HistoryId = '017D000000ESURXIA5']);
4Database.delete(fha);Alternatively, delete field history archive data with the new SOAP call deleteByExample(). Declare an sObject containing the fields and values in the FieldHistoryArchive big object to delete. This example deletes all rows that meet the specified criteria.
1public static void main(String[] args) {
2 try{
3 //Create two sObjects to delete and place them in an array of sObjects to pass to the delete method
4 FieldHistoryArchive[] sObjectsToDelete = new FieldHistoryArchive[2];
5 FieldHistoryArchive fha_1 = new FieldHistoryArchive();
6 fha_1.setFieldHistoryType("Account");
7 fha_1.setParentId("001d000000Ky3xIAB");
8 Calendar dt = connection.getServerTimestamp().getTimestamp();
9 dt.add(Calendar.DAY_OF_MONTH, -7);
10 fha_1.setCreatedDate(dt);
11 fha_1.setHistoryId("017D000000ESURXIA5");
12 sObjectsToDelete[0] = fha_1;
13
14 FieldHistoryArchive fha_2 = new FieldHistoryArchive();
15 fha_2.setFieldHistoryType("Account");
16 fha_2.setParentId("001d000000Ky3xIAB");
17 fha_2.setCreatedDate(dt);
18 fha_2.setHistoryId("017D000000ESURXIA5");
19 sObjectsToDelete[1] = fha_2;
20 DeleteByExampleResult[] result = connection.deleteByExample(sObjectsToDelete);
21} catch (ConnectionException ce) {
22 ce.printStackTrace();
23 }
24}