---
title: "SOQL All Rows"
url: "https://blog.beyondthecloud.dev/tips/soql-all-rows"
category: "SOQL"
tags: "apex, debugging"
published: "2025-01-06"
---

# SOQL All Rows

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.

### SOQL ALL_ROWS Clause

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).
