• Home
  • Cloud
    • General
    • SaaS
    • BPaaS
    • PaaS
    • IaaS
    • Other Internet Hosted Applications
      • WordPress
        • WooThemes Canvas
          • WooThemes Canvas CSS
  • About me
  • Why Badly Wired?
  • Contact

Badly Wired

Cloud solutions, technical snippets and other goodies

alan
You are here: Home / Code snippets / Code to delete all images from WordPress or nearly all
  • Free business email hosting for 15 days »
  • The best selling cloud solution right here »
  • 20% off Google Apps for Work Promo Code UK»

Code to delete all images from WordPress or nearly all

10th November 2014 by Alan 2 Comments

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

PHP
1
2
// 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

PHP
1
2
3
4
5
6
7
$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
1
2
3
4
5
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
PHP
1
2
3
4
5
6
7
8
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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>';
     }
   }
}
?>

 

 

 

 

 

Filed Under: Code snippets, Tech Tips, Wordpress  

About Alan

I'm Alan, a principal consultant at Fullworks Digital Ltd, a web development / cloud systems consultancy

My day job consists of looking into clients' businesses systems and sorting out their technology and often managing the implementation of change. The focus being leveraging the cloud primarily through the Google business applications (Google Apps for Work) as well as a significant amount or WordPress work.

I started as a professional programmer in 1979 and had been involved with the IT of business technology in virtually every area that exist.

My personal blog is my aide memoire of the many interesting facts that I come across. As I spend a lot of time gathering parts of solutions from the internet and assembling them into my own solutions, and also just learning how to do things, this blog is primarily my 'note book' and a way of giving something back to the online community that has helped me extensively.

Comments

  1. Pascal Roget says

    14th January 2017 at 1:39 am

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

    Reply
    • Alan says

      24th May 2017 at 10:24 pm

      Ha Ha

      Reply

Leave a Reply Cancel reply

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

Categories

  • Applications
  • Cloud
    • General
    • Google Cloud
    • IaaS
    • Other Internet Hosted Applications
      • Wordpress
        • WooThemes Canvas
        • WooThemes Canvas CSS
    • SaaS
  • Code snippets
  • Discounts
  • Genesis
  • Google Apps for Works
  • Linux
  • News
  • SEO
  • Server setup
  • Services
  • Tech Tips
  • Uncategorised
  • Useful Images
  • Useful Stuff

Tags

background jobs beadcrumbs bind brandings Cache canvas Centos chrome css fail2ban Find firefox Flash fraud genesis gocardless godaddy Google google maps hackers internet explorer javascript KashFlow Linus linux Magento mapquest maps microsoft mysql news nohup php plugin plugins queens diamond jubilee replace SED SEO skype Varnish Virtualmin Webmin woothemes Wordpress

Subscribe to my Newsletter

Sign up to my occasional newsletter

 

Affiliate Notices

This site is free to use, but hopes to cover some costs through affiliate income, some products and links are affiliates and may earn the site advertising income.

This site is a participant in the Amazon EU Associates Programme, an affiliate advertising programme designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.co.uk.

  • Privacy Policy
  • Account
  • Register
  • Memberships

Copyright © 2018 · Badly Wired

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy