1

As per Salesforce documentation 'Files' (ContentDocument) can be deleted only by the user who created them.
In order to provide access to delete a file created by other users, Salesforce recommends giving 'Modify All Data' to users which of course we can't give.

Alternatively, we are looking to create a custom Lightning component which would allow deletion of a file.
The question is how should we process delete request in apex?
Option 1: Just delete the 'ContentVersion' record.
Option 2: Delete the 'ContentVersion' then 'ContentDocumentLink' and then 'ContentDocument'.

Are there any other options? The goal here is not to just delete the link to the file but to entirely delete the file so that we save on space as well.

1

You can directly delete the content document from apex class which is without sharing and pass the IDs from component. Below method for example:

public without sharing class uploadController {

    @AuraEnabled
    public static String deleteDocuments(List<String> docIds){
        try{
            delete [SELECT Id FROM ContentDocument WHERE Id=:docIds];
            return 'SUCCESS';
        }
        catch(Exception ex){
            throw new AuraHandledException(ex.getMessage());
        }
    }
}

When a content document is deleted, its child objects like versions, links, comments etc are also deleted.


Here is the generic component which you can use for exactly this functionality - https://github.com/sasank-sfdcdev/public/tree/master/src/lwc/upload - Also this can be used in both AURA and LWC (pls note that this is in progress)

Using this component, you can:

  1. Get the files owned by current user

  2. Search for files

  3. Use this in scenario where file has to be uploaded while creating any record like account/case etc.

Note that this uses another component which you can download from same location.

You can use it like below:

<template>
    <c-upload parent-id={parentId}
              label="Upload Files"
              accept=".jpg,.jpeg,.pdf,.png">
    </c-upload>
</template>

You will get the UI as below:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.