Skip to Content
Menu
This question has been flagged
5 Replies
9922 Views

We're trying to clean up our filestore since its gradually growing and way beyond 100GB right now.
We are removing the avatar from the res.partner record, then observe that the 'image' related records are deleted automatically from the ir_attachment table.

However the files in the filestore related to the ir_attachment that is deleted remain in the filestore.
Do they ever get deleted, or are permanent?


The "Base: Auto-vacuum internal data" doesn't seem to do the job.

Avatar
Discard
Best Answer

I actually have a interesing case. I currently run v15 on odoo.sh and I'm exporting database to try to import it with filestore to different machine for testing and I get 'virus' warning on some file in the odoo db dump.

I was able to locate the phisical file with it's name from antivirus and tried to search for the file in ir.attachment table using the field "Stored Filename" (technical name of field 'store_fname'). Result is the file name that is physically in database dump is not found in the ir.attachment, but the file is physically in the filestore folder when doing db backup.

This is where it gets interesting... in pararel I'm tring to migrate to v17 and have my staging running with v17 version with the same database backup, and there I can find the file in ir.attachment based on it's "Stored Filename".

I want to remove the file from v15 to continue migration, but I cannot locate it.

any ideas?

Avatar
Discard
Best Answer

The scripts, written below, are missing one very important part.
If you use method search​ on model: ir.attachment​ without defining in domain fields: res_field​ or id​, it will exclude searching files saved in image fields, i.e profile photos of the users/partners

See: https://github.com/odoo/odoo/blob/fa20b5f2fed9e683c420798dce506a86fd5e511d/odoo/addons/base/models/ir_attachment.py#L527
We must change it, and add some workaround: ('id','!=',0)
self.search([('store_fname', '=', folder + '/' + file),('id','!=',0)])

Avatar
Discard
Best Answer

# Working in production >= odoo12


import os

def cleanup_filestore(self):
# Get all attachments in the filestore

# Get the current database filestore
filestore_path = self._filestore()
# Get all folders from filestore_path
filestore_folders = os.listdir(filestore_path)
for folder in filestore_folders:
# Get all files per folder
folder_files = os.listdir(join(filestore_path, folder))
# Iterate over files
for file in folder_files:
attach_exist = self.search([('store_fname', '=', folder + '/' + file)])
print(str(attach_exist))
if len(attach_exist) == 0 and folder in file:
​ # folder in file is to check if it is a file created by odoo and to prvent to delete other
os.unlink(join(filestore_path, folder, file))


Avatar
Discard
Best Answer

In Odoo, the "Base: Auto-vacuum internal data" module is designed to periodically delete old records from the database to free up space and improve performance. However, this module does not delete files from the filestore that are no longer referenced in the database.

To clean up the filestore, you will need to manually delete the files that are no longer needed. One way to do this is to use a script that iterates over the ir_attachment table and checks which files are no longer referenced in the database. You can then delete the corresponding files from the filestore.

Here is an example of how you might implement this in Python:

Copy codeimport os

from odoo import api, models

class IrAttachment(models.Model):
    _inherit = 'ir.attachment'

    @api.model
    def cleanup_filestore(self):
        # Get all attachments in the filestore
        filestore_path = self.env['ir.config_parameter'].get_param('ir_attachment.location')
        filestore_files = os.listdir(filestore_path)

        # Iterate over all attachments in the database
        for attachment in self.search([]):
            # Check if the attachment's file is in the filestore
            if attachment.store_fname in filestore_files:
                # Check if the attachment is referenced in the database
                if not self.search([('store_fname', '=', attachment.store_fname)]):
                    # The attachment is not referenced in the database, so delete the file
                    os.unlink(os.path.join(filestore_path, attachment.store_fname))

You can run this script periodically to clean up the filestore, or you can run it manually as needed.

I hope this information is helpful. If you have any further questions about managing the filestore in Odoo, please don't hesitate to ask.


Avatar
Discard

I suspect this comment to be written by ChatGPT. Do not run the code that is mentioned.

the idea is sensible but it should iterate through files NOT db entries.

Best Answer

Hi, how do you run it manually? thanks

Avatar
Discard

You can run it manually by using odoo shell (type ./odoo-bin shell or odoo shell in your terminal), you need to put line by line starting with filestore_path = self.env['ir.config_parameter'].get_param('ir_attachment.location') respecting the indentation

Also you can create a cron with the function and executing it manually

Related Posts Replies Views Activity
1
Feb 25
808
0
Nov 23
2798
1
May 22
5125
0
Mar 22
3652
0
Oct 21
2239