Remove old versions of files

Permalink
I have a Concrete 5.6.3.1 site in production that has a bunch of PDFs for each product on the site. When we change the PDF periodically, we replace the file in the File Manager using the "replace" function. However, the old version of the file remains on the filesystem. Because these old versions of the files are left around, Google has the old file url in its index, and people see the old version of the file when they click links from Google.

I tried deleting the old file versions from the File Manager, but this appears to only delete from the database, and not from the file system.

Is there any way to go through and remove the old physical files?

blakeage
 
jasteele12 replied on at Permalink Reply
jasteele12
Hi Blake,

Since you already deleted the files from the file manager I'm not sure this will help. You may have to iterate through the /files (excluding cache, tmp, etc) and check that each file is not referenced in the database and then unlink them..

Here's a /tools file that does work otherwise - be sure to read carefully - you must be logged in as Super Admin before running it in the browser.

This could be modified to run from a cron job...

If this helps, I accept Beer donations ;) John
<?php /* tools/unlink-old-versions.php - call like - http://yoursite.com/tools/unlink-old-verions... -  no .php */
/**
 * This legacy concrete5 tools file deletes and UNLINKS all non-active file versions.
 * 
 *    *** MAKE SURE YOU HAVE A BACKUP OF YOUR FILES AND DATABASE *** - You have been warned!!!
 * 
 * @author John Steele <john@steelesoftconsulting.com>
 * @copyright Copyright (c) 2015, John Steele, All rights reserved worldwide.
 * @link http://steelesoftconsulting.com/...   http://steelesoft.net/concrete5...  
 * @version 0.9
 */
defined('C5_EXECUTE') or die('Access Denied');
error_reporting(E_ALL & ~E_NOTICE);   ini_set('display_errors', 'On');
$u = new User();
if(!$u->isSuperUser()) { die('You are no Superman!'); }   // cheap security
blakeage replied on at Permalink Reply
blakeage
Thanks, this definitely helps. I was kind of afraid I'd be stuck writing something like this. It will work, until I replace more files, I'll have to run this every time, which is really inconvenient.

I'll probably turn it into an C5 "automated job", so it can be run easily from the dashboard.
jasteele12 replied on at Permalink Reply
jasteele12
You're welcome. If you do turn this into a c5 job it would be nice to share a copy here.

You could of course pass in a $_GET auth token, program it to run as the Super Admin and run it from a cron job on the host with wget or curl...

The second problem would be to handle the old Google requests and redirect to the active version.
blakeage replied on at Permalink Reply 1 Attachment
blakeage
I've attached a C5 job that will delete all old file versions, including the physical file from the server.

PLEASE NOTE: If you have hard-coded links to old versions of the file anywhere in the CMS, you will have to fix these links, as they will break after running this script.

I assume no responsibility for this script! Backup your files and test before using this in production.
buurvrouw replied on at Permalink Reply
buurvrouw
Does this work on 8.1.0 installs too?
blakeage replied on at Permalink Reply
blakeage
I haven't tested it on newer versions of Concrete. I would back up everything and test it in development first.