deleteS3Files
Deletes one or more objects directly from the storage bucket (S3/GCS), by storage key. This method never touches Salesforce records — it only removes the stored bytes.
See https://cyangate.atlassian.net/wiki/x/BAA63g for an explanation deleteFiles vs deleteS3Files.
How to call this method
global static void deleteS3Files(List<String> awsLocationList, List<String> versionIdList)
Parameters
|
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
|
|
Yes |
The storage key(s) of the object(s) to delete — this is the value of the file record's |
|
|
|
No |
One version ID per entry in |
Return value
Returns nothing (void) on success. Throws SDriveException if the storage service reports an error (e.g. object not found, invalid version ID, permission error) — wrap the call in a try/catch if you need to handle that gracefully rather than let it bubble up.
Before you call this
-
All keys passed in a single call are assumed to belong to the same bucket — the bucket's settings are resolved from the first key in
awsLocationList. If you pass keys for files that live in different buckets in one call, the wrong bucket's credentials/endpoint may be used for the rest, and those deletes can fail silently or hit the wrong bucket. Group keys by bucket and call this once per bucket if you're not certain all your files share one. -
This does not update or delete the Salesforce record. If you don't clean up the record yourself, it will be left pointing at a file that no longer exists.
-
This is destructive and cannot be undone.
Example
// Look up the storage key (and version, if applicable) for the files you want to purge
List<cg__CaseFile__c> filesToPurge = [
SELECT Id, cg__Key__c, cg__Version_Id__c
FROM cg__CaseFile__c
WHERE Id IN :fileIdsToPurge
];
List<String> keys = new List<String>();
List<String> versionIds = new List<String>();
for (cg__CaseFile__c f : filesToPurge) {
keys.add(f.cg__Key__c);
versionIds.add(f.cg__Version_Id__c); // fine to add null entries if the bucket isn't version-enabled
}
try {
// Removes the files from the storage bucket only.
SDriveTools.deleteS3Files(keys, versionIds);
// deleteS3Files never touches Salesforce records — if you want the
// records gone too, delete them yourself:
delete filesToPurge;
} catch (SDriveException e) {
System.debug(LoggingLevel.ERROR, 'Failed to delete from storage: ' + e.getMessage());
}
Cleaning up an orphaned object with no Salesforce record
// You already know the storage key of an object that has no matching Salesforce
// record (e.g. found via a bucket listing) and just want it gone from storage.
List<String> orphanedKeys = new List<String>{ '001XX000003DHPh/a00XX0000004Cx1AAE/old-file.pdf' };
try {
SDriveTools.deleteS3Files(orphanedKeys, null);
} catch (SDriveException e) {
System.debug(LoggingLevel.ERROR, 'Failed to delete orphaned object: ' + e.getMessage());
}