How to make attachment "deleted = 1" in the DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Laimonas
    Senior Member
    • May 2021
    • 197

    How to make attachment "deleted = 1" in the DB

    Hi there,

    I have a question. If I delete a Task (or any other record) that contains an attachment, that attachment in the DB still remains "deleted = 0". Then cleanup Job does not delete that file from the CRM upload folder. If change the Attachment manually in the DB from "deleted = 0" to "deleted = 1", then cleanup Job deletes that attachment from the CRM upload folder.

    So my question is how to make the attachment in the DB to be "deleted = 1" when I delete an attachment in the UI?

    My final goal is to let the cleanup job delete attachments from the CRM upload folder.
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Hi,

    You can write a hook for the Attachment entity and force the value of "deleted" to 1 but I don't have experience playing with the "Attachment" entity to know the consequences of doing this manually.

    Espo is pretty logical so there is probably a good reason why when you "delete" an attachment through the GUI the deleted flag in the database table stays 0.

    Consider also that the Attachment record only contains the reference to the actual attachment (image, etc) file stored in your server, so if you want to actually remove the attachment you need to delete this file as well.

    My suggestion would be to check the backend code base for Attachment and start there.

    If you figure it out, please post your solution here as contribution to make the forum a better source for everyone.

    Comment

    • shalmaxb
      Senior Member
      • Mar 2015
      • 1602

      #3
      In this context I have a question (I do not want to hijack this thread though): As far as I know, the deleted references will be purged later from the database. Is that right?
      An if so, will there be the referenced files deleted as well?.
      I am asking this, because I have installations with lots of images, during the time I also had a lot, that I had to upload again (because of errors the references were lost) and now old, useless images occupy server space.
      If these files are not deleted, how can one get rid of them?

      Comment


      • Laimonas
        Laimonas commented
        Editing a comment
        but how about with Attachments? Let's get back to the beginning - If a Task contains an attachment and you delete the whole Task, the Task becomes "deleted = 1", but the Attachment stays "deleted = 0" in the DB.

      • shalmaxb
        shalmaxb commented
        Editing a comment
        All files are referenced in the table attachments, as I understand there is no difference, if a file is in a task, an email, an entity or in the documents.

      • Laimonas
        Laimonas commented
        Editing a comment
        Yes, the Attachments are located in a separate table. But my idea is, that if I delete a Task (or Case/Email/Account etc that contains an attachment), I want both of them to be deleted in one click (in DB: Task - "deleted = 1", Attachment - "deleted = 1"). At the moment Attachment stays "deleted = 0" and it will be there forever and use server space. If I want manually delete an attachment (make in DB Attachment "deleted = 1") I have to go to Administration -> Attachments -> find that attachment which was in the deleted Task and remove it from there. That's totally a waste of time. My goal is to make them both "deleted = 1" in one click when I delete a Task (or Case/Email/Account etc that contains an attachment).
    • item
      Active Community Member
      • Mar 2017
      • 1476

      #4
      Hi,
      one job run every week or ? :

      PHP Code:
      $attachments = $entityManager->getRDBRepository('Attachment')->find();
      
      foreach ($attachments as $attachment)
      {
        $entity = $entityManager->getRDBRepository($attachment->get('parentType')) ->where([ 'id' => $attachment->get('parentId'), ]) ->findOne();
        if (!$entity){
           unlink( 'data/upload/' .$attachment->get('id') ); // better use fileManager class.
           $entityManager->getRepository()->deleteFromDb($attachment->get('id'));
         }
      } 
      
      something like so ? but to rewrite, not tested.. it's just the idea
      when you can't open the front door... try the door on back-end
      of course, it's maybe a feature request for cascade delete on some criteria :
      if entity have attachment.. so cascade delete
      Last edited by item; 11-24-2021, 09:46 PM.
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      • shalmaxb
        Senior Member
        • Mar 2015
        • 1602

        #5
        I had a look in the various threads about this problem. I don`t know, what of the for years discussed behaviour is valid for now. I see, that a deleted attachment is flagged as delete=1 in the database, but the file stays in the upload folder. In past discussions it was told, that these files will be deleted after 30 day by a scheduled job (default) in espoCRM. But I don`t know, if that still is valid and what would be the reason to keep the file, that I actively deleted, in my app. Maybe there is a reason, but I don`t see one.

        Would be nice, if anybody from the developers or anybody with more insight could clear that. Thank you in advance.

        Comment

        Working...