Code to delete all images from WordPress or nearly all

If you find this free website useful – why don’t you support this with a donation? It is easy…. read more ….

The other day I had a bit of a nightmare with a plugin that looped and created multiple images, in fact thousands of them.

I got bored with trying to delete them manually so wrote some code quickly.

The first thing was how to write code that runs using wordpress, you can use  technique simply to load wordpress

// Include the wp-load'er
include('wp-load.php');

once loaded you can then use all the WordPress functions

First I want to get all the attachments, these are just post types so it is matter of calling get_posts

$args= array(
  'post_type'      => 'attachment', // obvious
  'posts_per_page' => -1            // get them all 
);

// get all attachments post ids
$posts = get_posts( $args );

Then a loop getting the image data

foreach ($posts as $post_id) {
   // get an array of image data
   $image_attributes = wp_get_attachment_image_src( $post_id->ID );
   // do stuff
}

In this case I wanted to only delete images that had a certain string so I can do this with strpos to identify the right image by string and wp_delete_attachment to delete it.  Of course you could use preg_match for complex matching http://php.net/manual/en/function.preg-match.php

if (strpos($image_attributes[0], 'mystring') !== false){
   echo 'Image Found : '.$image_attributes[0];
   if (false === wp_delete_attachment( wp_delete_attachment( $post_id->ID, true ) ) ) {
       echo ' and delete failed!<br>';
     } else {
       echo ' and delete succeeded!<br>';
     }
   }

 

Add it all together and you get something like this. Be careful, use at your own risk, and always backup your database first. (I’d recommend commenting out the delete first too, to make sure )

<?php
/*  create this code in a file in the main wordpress directory e.g. delmedia.php
    and access it via mydomain.com/delmedia.php
*/
// Include the wp-load'er
include('wp-load.php');
$args= array(
  'post_type'      => 'attachment', // obvious
  'posts_per_page' => -1            // get them all 
);

// get all attachments post ids
$posts = get_posts( $args );
foreach ($posts as $post_id) {
   // get an array of image data
   $image_attributes = wp_get_attachment_image_src( $post_id->ID );
   if (strpos($image_attributes[0], 'mystring') !== FALSE){
   echo 'Image Found : '.$image_attributes[0];
   if (false === wp_delete_attachment( wp_delete_attachment( $post_id->ID, true ) ) ) {
       echo ' and delete failed!<br>';
     } else {
       echo ' and delete succeeded!<br>';
     }
   }
}
?>

 

When you come to run this, it is best to run as a command line ( or via cron ) e.g.  /usr/bin/php deleteimages.php  as php on the command line won’t time out, whereas running from a web page e.g. https://example.com/deleteimages.php  will be subject to php time out limits and if you are deleting lots you will need to keep re running

 

 

 


Posted

in

, ,

by

Tags:

Comments

2 responses to “Code to delete all images from WordPress or nearly all”

  1. Pascal Roget avatar

    Good thing badlywired.com/delmedia.php returns a 404 🙂

    1. Alan avatar
      Alan

      Ha Ha

Leave a Reply to Pascal Roget Cancel reply

Your email address will not be published. Required fields are marked *