Forum

Thread tagged as: Question, Problem

Assets not being deleted automatically

I want old assets that are not in use to be deleted immediately. I've reduced the history stack by adding this to config.php: define('PERCH_UNDO_BUFFER', 1);

The assets are not marked as library assets. But after removing them from a content region, they remain visible in Assets, and the files remain in the resources folder.

This is a problem because old assets (PDFs) can continue to be accessed by website users unless they are actually deleted from the server. A Google search continues to show the old PDF in search results, the url for the old PDF is still valid and users can still access the old file. The old url will eventually drop out of Google, but this still leaves a window during which the old PDFs are accessible.

I realise I could delete the old assets manually, but this would require giving the client admin rights and it's an extra little task to do every time a new PDF is uploaded.

How can I set this up so that old assets are deleted automatically?

Here's my template - it has a repeater, with each item including a file (a restaurant menu PDF) and a thumbnail image:

<perch:before>
<ul class="menus clearfix">
</perch:before>
<perch:repeater id="menus" label="Menus">
<li><a href="<perch:content id="file" type="file" label="File" order="2" />#view=Fit"><img src="<perch:content type="image" id="thumbnail" label="Thumbnail image" order="3" width="150" height="150" crop="true" />" alt="<perch:content type="text" id="desc" />" /></a><a href="<perch:content id="file" />#view=Fit"><perch:content type="text" id="desc" label="Title" order="1" required="true" title="true" /></a></li>
</perch:repeater>
<perch:after>
</ul>
</perch:after>

Short diagnostics:


Perch: 2.8.32, PHP: 5.5.38, MySQL: 10.0.27-MariaDB, with PDO Server OS: Linux, litespeed Installed apps: content (2.8.32), assets (2.8.32), categories (2.8.32), perch_blog (4.5.4), perch_forms (1.8.2), perch_gallery (2.8.5) App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', 'perch_forms', 'perch_gallery' ); ?> PERCH_LOGINPATH: /cms PERCH_PATH: /home/oilyspubco/public_html/cms PERCH_CORE: /home/oilyspubco/public_html/cms/core PERCH_RESFILEPATH: /home/oilyspubco/public_html/cms/resources Image manipulation: GD PHP limits: Max upload 100M, Max POST 100M, Memory: 1024M, Total max file upload: 100M F1: 2edba60ed1f613d6dd804feb202456a2 Resource folder writeable: Yes HTTP_HOST: oilyspub.co.uk DOCUMENT_ROOT: /home/oilyspubco/public_html REQUEST_URI: /cms/core/settings/diagnostics/ SCRIPT_NAME: /cms/core/settings/diagnostics/index.php
Mark Melling

Mark Melling 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are the PDFs referenced in other content on the site, or only in this one place?

How many times have you edited the region since changing the size of the undo buffer?

No, the PDFs are only in this one region.

After changing the undo buffer I have tried saving the region a few times eg added a test PDF with a test thumbnail image, saved, deleted the entire repeater item, saved - and then repeated again with another test PDF + thumbnail. So a total of 4 saves and both of these test PDFs and the thumbnail images are still in Assets and still in the resources folder over 24 hours later.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have PERCH_CLEAN_RESOURCES or PERCH_DONT_DELETE_FILES configured in your config.php?

No - I don't have either of those.

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's curious then. Do you see your PDFs logged in the perch2_resource_log database table?

They don't appear to be in the perch2_resource_log database table.

The 2 test PDFs are in the the perch2_resource table - one has resourceID 239 and the other resourceID 247. But these two resourceIDs are not visible in the perch2_resource_log database table.

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's interesting. Either they were never logged, or Perch has already tried to remove them and somehow failed.

Do any of the non-deleted items appear?

Not clear what you're asking. But the same region has other items in the same repeater; and these contain PDF menus plus thumbnails. These haven't been deleted and they all appear on the site corectly.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Of the PDFs that have not been deleted, are those in the perch2_resource_log table?

I'm trying to establish if the PDFs are never being logged (and therefore not deleted) or if they're being logged and deleted but the delete fails.

Apologies. Yes, there are 5 other PDFs in the same repeater with resourceIDs 222 to 226, as shown in the perch2_resources table. Each of these resourceIDs has 2 lines in the perch2_resource_log database table.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. So it sounds like what's happening is that Perch is trying to delete the asset but it's not working for some reason. Does the process that PHP runs as have permission to delete files on your filesystem?

Have just checked this with my host, who says: "Yes PHP can delete files using the unlink() function to which this is not restricted in any way. On checking the /public_html/cms/resources folder it appears all the permissions are correct therefore I can see no reason why PHP would not be able to delete the files."

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so it sounds like it should work. In which case I don't have an immediate answer for you as to what is happening.

On the live site I've just tested creating a file (using fopen) and deleting a file (using unlink) and both worked. So PHP can delete files on that server.

I've been wondering whether anything is unusual about this situation: - I've renamed the Perch directory - the files are being added/removed in a repeater

Might either of those be causing a problem?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Unlikely to be an issue with the folder name, but it's possible it could be related to the repeater. I'd need to investigate the scenario to find out.

That would be great if you could investigate - thanks.

I've re-saved the repeater region with debug enabled and saw it deleted two of the thumbnail images. That's great, but it hasn't deleted the PDFs which were removed from content at the same time as those thumbnails.

Is it possible that PDFs are not being cleaned up but images are? And does that point to a solution?

Thanks again.

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's unlikely but if it's not working as expected then we don't discount anything.