Skip to content

How to Remove Outdated Images from WP All Export

Stop WP All Export from exporting images that were deleted from a vehicle but left behind in the Media library, by installing a small cleanup plugin that clears them before every export.

1 minute read

When exporting images with WP All Export, you may notice that deleted vehicle images are still there. A vehicle with 17 images can show 5 extra images in the export file, because those images were once associated with it and are still in the WordPress Media library. The result is incorrect image counts and incorrect exports.

Solution

Follow these steps to clean up the residual images and get accurate exports.

The cleanup deletes files permanently
The plugin below permanently deletes every attachment whose parent is a vehicle but which is no longer used in one of that vehicle's gallery fields. Back up your site and your Media library before you run an export with it active.

Create the cleanup plugin

  1. 1 Open the plugins folder

    Navigate to the wp-content/plugins directory in your WordPress installation.

  2. 2 Create the plugin file

    Create a new file named vehica-cleanup.php.

  3. 3 Paste in the code

    Paste the code below into the vehica-cleanup.php file.

vehica-cleanup.php
<?php
/*
Plugin Name: Vehica CleanUp
Version: 1.0.0
Plugin URI: https://tangiblewp.com
*/
 
add_action('pmxe_before_export', function () {
    $attachments = new WP_Query([
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
    ]);
 
    foreach ($attachments->posts as $attachment) {
        /* @var WP_Post $attachment */
        $assignedPostId = wp_get_post_parent_id($attachment->ID);
        if (!$assignedPostId) {
            continue;
        }
 
        $model = \Vehica\Model\Post\Car::getById($assignedPostId);
        if (!$model instanceof \Vehica\Model\Post\Car) {
            break;
        }
 
        $found = false;
 
        foreach (vehicaApp('gallery_fields') as $galleryField) {
            /* @var \Vehica\Model\Post\Field\GalleryField $galleryField */
            $imageIds = $galleryField->getValue($model);
            if (!in_array($attachment->ID, $imageIds, true)) {
                continue;
            }
 
            $found = true;
        }
 
        if (!$found) {
            wp_delete_attachment($attachment->ID, true);
        }
    }
});

Install and activate it

  1. 1 Open the Plugins screen

    Go to your WordPress admin panel (wp-admin), then to the Plugins section.

  2. 2 Activate Vehica CleanUp

    Find the plugin named Vehica CleanUp and activate it.

Once activated, the plugin performs the cleanup automatically before each export, removing the residual images.

Ask the people who build Vehica

We answer by email from support@tangiblewp.com. With Premium Support, we also fix your site when something breaks.

Was this guide helpful?