SOQL All Rows

SOQL ALL_ROWS Clause

You can use the SOQL ALL_ROWS clause for that. The resulting query includes the soft-deleted records (the ones in the recycle bin) with the results.

Do you need to query deleted records?

You can use the SOQL ALL_ROWS clause for that. The resulting query includes the soft-deleted records (the ones in the recycle bin) with the results.

Here’s an example that will help you understand and test this feature:

apex
Account activeAccount = new Account(Name = 'Active Acc');
Account accountToDelete = new Account(Name = 'To Delete');

List<Account> accounts = new List<Account>{ activeAccount, accountToDelete };
insert accounts;

delete accountToDelete;

Integer accountsNumber = [SELECT COUNT() FROM Account WHERE Id IN :accounts ALL ROWS];

Assert.areEqual(2, accountsNumber, 'Both accounts should be returned when using ALL ROWS clause.');

It can be useful for use cases like auditing, accidental removal mitigation, integrity checks, or debugging.

Note: you can’t use the ALL_ROWS clause via API (only Apex, my friend).

Text and code were extracted from the original slide. Plain-text version of the whole catalog