5

Using SOQL, how do I truncate an object or delete all rows. For example

DELETE (Select id FROM MyObject__c)

and

delete [select Id FROM MyObject__c]

and

TRUNCATE TABLE MyObject__c

All 3 example commands in the Developer console returns:

The query has to start with 'FIND' or 'SELECT'

3 Answers 3

6

You can't perform any DML using SOQL. The 'Q' stands for 'Query'.

If you want to use Execute Anonymous, then you can use:

delete [select id from myobject__c limit 10000];

However, this has a 10,000 row limit, so that might not be appropriate in all cases.

Salesforce can query 50,000 records at a time but due to governor limit it'll allow only 10,000 DML operation

The only way to perform a quick truncate would be to use the UI to initiate the truncate.

2
  • Thanks - That's a horrible limitation. But it is what it is.
    – PhillyNJ
    Mar 2, 2015 at 20:41
  • So there is no way to test this via Console and I eventually need to do this via SOAP, it looks like the best option is to use a Select() then Delete() salesforce.com/developer/docs/api/Content/…
    – PhillyNJ
    Mar 2, 2015 at 20:55
0

You can truncate a custom object.

For the Truncate option to show on custom object, do not forget to check ‘Enable Custom Object Truncate’ from Setup -> Customize -> User Interface as shown below

Or from an anonymous window, if your row count is less than or equal 10000, just run the code

DELETE[SELECT id FROM myCustomObject__c LIMIT 10000];
0

You can truncate custom objects in Salesforce using an internal feature called "Truncate". This feature is only available in classic. Few things you need to keep in mind - The truncate operation will fail if -

  1. There is any reference to other objects through lookup or master-detail relationships.
  2. The object is referenced in reporting snapshots.
  3. The object is having a custom index or an external ID.
  4. Have activated skinny tabled.
  5. Truncate is going to delete the records permanently. So you will not be able to recover records from Recycle Bin.
  6. A copy of the truncated object will be kept in the Deleted Objects list for 15-days.

Check out the blog post here and the video here

You must log in to answer this question.

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