[SalesForce] Error: Cascade dereferencing too many records

Let me briefly describe data model to provide more clarity over the target error.

There are 3 objects: X, Y, and Z. Z is related to Y via master-detail relationship. Z is related to X via lookup relationship. Y is related to X via lookup relationship.

Here is what I have in terms of data volume: a single X record, 9k Y records (all of them reference X via lookup field), and 98k Z records (12 per each Y record; all of them reference X via lookup field).

I get "Cascade dereferencing too many records: occurring when cascading to Y records." error every time I'm trying to remove X record. Let me just emphasize that X is not referenced via any master-detail relationship, which means that cascade deleting is not the case here.

I've practically figured out that this errors comes into picture in case of the combined total number of Y and Z records, referencing X record, is greater than 100k. The strange thing is the error above is preserved even in case of all Y and Z records get erased (so X record becomes the only record in the SF instance).

Things became even more weird when I had reproduced another use case. What I did is created a single X record and 180k Y records, referencing X record (no Z records at all). In that case X record removal goes smooth despite on the fact that the number of child records is greater than in the previous example.

Has anyone faced similar errors? Any ideas on reason and possible workarounds?

Thanks in advance!

Best Answer

Here is the reply from SF support: "There is a hard limitation on the Salesforce side. The delete operation is only supported for a records which has less than 100k related records. No matter whether it's referenced via lookup or MD relationship."

Unfortunately, they avoided from answering why there are some cases when the record, which is referenced by more than 100k related records, can be removed w/o errors, so that remains a secret.

However, I got an answer of why target record can't be removed even after the related records been deleted and erased from the org recycle bin. The thing is there are 3 stages of deletion:

  • Stage 1 - Deleted and sent to Recycle Bin or soft deleted - Data in this stage can still be recovered from the recycle bin or via the API via an undelete() call.
  • Stage 2 - Emptied from the Recycle Bin or hard deleted - Data in this stage cannot be recovered from the recycle bin or undeleted but can be exported from the API using Export All via Data Loader or queryAll() using alternative API client tools. Records can skip the recycle bin and be put in this state directly by using the 'Hard Delete' option in the Data Loader.
  • Stage 3 - Physically/Permanently deleted - Data in this stage has been wiped from database. ​

This 3-stage deletion process helps prevent accidental deletion by end-users. As a SF admin/developer you can only reach out the 1st and 2nd stages, while the 3rd one can only be initiated by some scheduled internal SF process or by SF support agent after submitting a case to request a Physical Delete. That's why even after the related records gets removed, the target record will be kinda "locked" from deletion for some time (up to 3 days from my experience).