5

Does Database.emptyRecycleBin work as intended under test conditions?

The test below would suggest not as the actual row count is one instead of an expected zero. Have I missed something? I have already tried wrapping the act with Test.StartTest / Test.StopTest on the assumption that purges run with a low priority asynchronously but to no avail.

@isTest
private class PurgeAccountTest{

    private static testMethod void canPurgeAccount() {
        // arrange
        Account account = new Account(Name = 'Mike');
        insert account;
        List<Id> accountIds = new List<Id>{ account.Id };
        delete account; 

        // act
        Database.emptyRecycleBin(accountIds);
        List<Account> accountsAfterPurge = [SELECT id, Name FROM Account  ALL ROWS];

        // assert
        Integer expectedAfterPurgeCount = 0;
        System.assertEquals(expectedAfterPurgeCount, accountsAfterPurge.size());
    }    
}

1 Answer 1

7

After adding a few more assertions to double check the result of this operation, including one for DML usage, since the documentation states the Database.emptyRecycleBin method consumes a DML operation. And also trying the various overloads of this method.

I am of the conclusion this is a platform bug, one you should attach to a support case and see if you can get Salesforce to log it as a known issue.

@isTest(SeeAllData=true)
private class PurgeAccountTest{

    private static testMethod void canPurgeAccount() {
        // arrange
        Account account = new Account(Name = 'Mike');
        insert account;
        List<Id> accountIds = new List<Id>{ account.Id };
        delete account; 
        List<Account> accountsAfterDelete = [SELECT id, Name, isDeleted FROM Account where id = :account.Id ALL ROWS];
        System.assertEquals(true, accountsAfterDelete[0].isDeleted);

        // act
        System.assertEquals(2, Limits.getDmlStatements());
        List<Database.EmptyRecycleBinResult> results = Database.emptyRecycleBin(new List<Account> { account });
        // List<Database.EmptyRecycleBinResult> results = Database.emptyRecycleBin(accountIds);
        // Database.EmptyRecycleBinResult result = Database.emptyRecycleBin(account);
        Database.EmptyRecycleBinResult result = results[0];
        System.assertEquals(true, result.isSuccess());
        System.assertEquals(null, result.getErrors());
        System.assertEquals(account.Id, result.getId());
        System.assertEquals(3, Limits.getDmlStatements());
        List<Account> accountsAfterPurge = [SELECT id, Name, isDeleted FROM Account where id = :account.Id ALL ROWS];

        // assert
        Integer expectedAfterPurgeCount = 0;
        System.assertEquals(expectedAfterPurgeCount, accountsAfterPurge.size());
    }
}
2
  • Thanks very much for the validation, Andrew. I spent a fair bit of time wondering where I went wrong so I am more than happy to be able to move on (after logging a case of course).
    – max
    Feb 6, 2014 at 16:51
  • this is still true as of V44 - see salesforce.stackexchange.com/questions/27180/… for possible explanation (recycle bin is emptied via async process)
    – cropredy
    Jan 7, 2019 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .