Attachement again (the last one)

What is the right way to delete an attachment, I think that the file deleted stays in the File system.

Removing files is not supported yet, but there is a merge request on this subject:

would it not be simpler that when you delete the file in the ir_attachement view, you get the file_name (the hash) then look to see if he is “duplicated” and it is use elsewhere (so not removing it) and if it is the last one just delete from disk ?

We deemed your idea too complicated to implement that’s why it was never implemented.

I read the proposal to commitment … you think my idea to complicated ???

pseudo code

def supress attachement (attachement_id)
    get hash of file(attachement_id->file_id)
    count ir.attachement[(hash_of_file)
    count=1 
        delete /var/lib/tryton/tryton/substr(0,2,hash)/substr(2,4,hash)/hash
    delete ir._attachment(id)

I guess it is less than 50 lines in proteus (with a bunch of try-except)

i’ll implement it in my install

In fact the file can be linked to another fields.Binary in another object and it will have the same hash, so you will have to check every Binary field in every table.

Also you can not delete directly the file because your transaction can rollback.

So yes, maybe in your specific use case your solution is simple enough but there are other usecases that we have to take into account.

there is a table ir_attachement
id | copy_to_resources | create_date | create_uid | data | description | file_id | link | name | resource | type | write_date | write_uid
----±------------------±---------------------------±-----------±-----±------------±---------------------------------±-----±---------------------------------±------------------±-----±-----------±----------
66 | | 2025-12-18 18:23:49.392712 | 1 | | | 45a1d192d1c49c94dfc6ca6f319854de | | contrat-manger-minou_le_chat.pdf | party.party,25561 | data | |

in my config files are stored on disk that pef has 45a1d192d1c49c94dfc6ca6f319854de as hash

Att=Model.get(ir.attachement)
to_del=Att.find([('id,'=',args.todelete)
the_atts=Att.find([('file_id','=',to_del.id)
hash=to_del.file_id
if (len(the_atts))>1:
        pass
else:
        os.remove (/var/lib/tryton/tryton/substr(0,2,hash)/substr(2,4,hash)/hash)
to_del.delete()

I am not arguing with all binary files in whatever model, just supress attachments in view

when you suppress a party, a product, a move, an attachement, there is no rolling back

You do you, I don’t really care.

I’m trying to help you by pointing out:

  • What you want to do is not possible currently
  • But a merge request will make it possible soon

Then you’re arguing with me because you have a better idea which work only in your specific use case (ie: through proteus and only taking into account the attachments).

I will stop this useless discussion.

i am not arguing with you because of i-do-not-know-why
i am arguing because i asked
What is the right way to delete an attachment

And you says it is complicated and somewhere in the futur (version 8 - migration forced) there will have a possibility to delete a binary.data within a configured laps time.

the question was : how to delete a file in the file system when the attachment is delete from a object.

Yes your solution is probably better (more use case, and you can revoke your supression) but K.I.S.S is my religion. and that “bad” solution is what i need.
it is proteus code because i am working with proteus at the moment and have no computer to test so it is some kind of fast draft.

thanks for responding