Is it possible to remove attachments from Emails but keep email records? [SugarCloud]

----------------

Requirement is to write a scheduler and remove the attachments on daily basis from archived emails which are linked to cases (specific status).

----------------

I was thinking to delete the note record based on email id, will it remove the attachment as well on cloud instance?

Please anyone share if there is any cloud compatible code to achieve this as a best practice and remove the attachments gracefully without deleting the email.

Thanks.

  • It's possible.

    At its most basic an Email attachment is just a Notes record related to an Email.

    The Emails bean has a field called total_attachments which tells you how many attachments are related to the Email

    So you can write a scheduler that uses SugarQuery to find all the emails with total_attachments > 0 and the specific status you want.

    Then loop through those Emails Beans  and for each one find the related notes and mark_deleted, something like:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    $link = 'notes';
    if ($emailBean->load_relationship($link)) {
    $relatedNotesBeans = $emailBean->$link->getBeans();
    foreach($relatedNotesBeans as $attachmentBean){
    $attachmentBean->mark_deleted($attachmentBean->id);
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    This will mark those attachments (notes) as deleted = 1

    And then when the scheduler runs at the beginning of each month to clear deleted records the notes will be permanently deleted, as will the corresponding files.

    FrancescaS

  • Hi  

    Thanks for confirmation, my understanding was also same.

    However before posting the question here , Sugar support told me that attachment will not be removed thats why i asked here and they still not recommending it and saying same.

    May i know if there is any hidden scenario which i don't know?

  • Hello  , 

    Thanks for the additional insight!
    Was the information from support related to a specific Sugar version or perhaps issue?
    I just tested this on version 25.1, and calling mark_deleted() does in fact remove the attachment from the filesystem.

    Based on that, I believe Francesca’s approach would also result in the files being removed from the filesystem.

    Cheers,

    André 

  • Hello  

    Thanks for endorsing the approach.

    I have got the clarification from support team and they have mentioned now that the deletion of records via script will remove the files.

    All good now.