apex – Deleted CampaignMember records are still coming up in the SOQL query result

Some CampaignMember records are deleted and after that when I run a SOQL query without any filter criteria in code or query editor, it fetches all the campaign members with deleted ones, also their isDeleted is set as false, and after that when I try to open their detail page it shows as ‘Data not available.

When I run the SOQL query i.e.

SELECT Id FROM CampaignMember

But it gives me the result(for e.g.) as 5 records but from these 5 records, 4 records are already deleted, but they are coming in SOQL query result, as well in Apex code. when I try to open these 4 record detail pages, it shows ‘Data not available’.

And when I run this SOQL query

SELECT Id, CreatedDate FROM CampaignMember

then it gives the result of 1 record. that exists in the database

Deleted records should not appear in a standard SOQL query.

As CampaignMember does not support UNDELETE call so can’t make this call on those records(can’t be find in recycle bin)
When I try to delete them manually, shows me the error

System.DmlException: Delete failed. First exception on row 0 with id <ID>; 

first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Read more here: Source link