born05/craft-assetusage

Assets still available in database

Closed this issue · 6 comments

Thank you for creating this plugin; however, I encountered an issue while using it for my project. Due to the large number of images (+40k) in the project, I need to set a limit in the code to address performance issues.

The getUnusedAssets function includes the following query:

$subQuery = (new Query())
    ->select('id')
    ->from(Table::RELATIONS . ' relations')
    ->where('relations.targetId=assets.id')
    ->orWhere('relations.sourceId=assets.id');

$query = (new Query())
    ->select(['id', 'filename'])
    ->from(Table::ASSETS . ' assets')
    ->where(['not exists', $subQuery])
    ->limit(1000);

While running the function successfully removes the first 1000 assets, I am unable to list the next 1000; it keeps displaying the initial set. Upon investigation, I discovered that these assets still exist in the database. I'm now exploring the possibility of deleting these assets from the database as well. Is there a way to achieve this?

@remcovb, are you using craft assetusage/default/delete-unused? That should actually delete the files and database records (please be really carefull with using this, not every fieldtype is using relations and can be missed).

Anyway, when using a limit, i would expect running the same thing a second time it would get a 1000 of the remaining assets. You could also try to emptying the trash using craft gc/run (this also empties the trash for entries etc).

Thanks for the quick response, I actually make use that action. Below you find and example with the limit set to 1:
Group 1

I've emptied the trash a couple times, but i keep getting the same issue.

@remcovb that's weird. We just call Craft::$app->getElements()->deleteElement($asset); exactly as craft does when deleting an asset from the CP. I'll look into this.

Are there any error logs? I just tested this and i did kept getting the deleted assets, until cleaning out the trash (craft gc/run). They didn't return after that.

@remcovb can you test this in 3.3.0 ? It doesn't have the limit, but it does ignore deleted assets.

@roelvanhintum the update appears to have fixed the issue! I'm still testing around, but at first glance everything seems to work great, appreciate it!