WordPress Plugin: Empty Blog

In doing some work with the WordPress.org importer, I found myself frequently having to empty a .org blog. For blogs with lots of posts and attachments, this is a pain, as you have to page through the items and delete 20 at a time. This little plugin handles all of the deletions for you (beware, it’s destructive and the deleted data is irretrievable).

<?php
Plugin Name: Empty Blog
Plugin URI: https://daryl.wordpress.com/wordpress/empty-blog/
Description: Empty your blog of posts, pages, media, and categories.
Version: 0.1
Author: Daryl L. L. Houston
Author URI: https://daryl.wordpress.com/
License: GPL2
*/

/*  Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

add_action('admin_menu', 'empty_blog_menu');

function empty_blog_menu() {
	add_options_page('Empty Blog Options', 'Empty Blog', 'manage_options', 'empty-blog-id', 'empty_blog_options');
}

function empty_blog_options() {
	if (!current_user_can('manage_options'))  {
		wp_die( __('You do not have sufficient permissions to access this page.') );
	}
	if( isset( $_POST['empty'] ) ) {

		// Delete posts
		echo "</pre>
<h3>Deleting posts/pages</h3>
<pre>
n";

		$options = array(
			'numberposts'     => 50,
			'offset'          => 0,
			'orderby'         => 'post_date',
			'order'           => 'DESC',
			'post_type'       => 'post',
			'post_status'     => 'publish'
		);

		$statuses = array ( 'publish', 'draft', 'trash', 'inherit' );
		$types = array( 'attachment', 'post', 'page');

		foreach( $types as $type ) {
			foreach( $statuses as $status ) {
				$options['post_type'] = $type;
				$options['post_status'] = $status;
				delete_posts( $options );
			}
		}

		// Delete categories
		echo "</pre>
<h3>Deleting categories</h3>
<pre>
n";
		$cats = get_all_category_ids();
		foreach( $cats as $cat ) {
			wp_delete_category( $cat );
			echo "Category: $cat
";
		}

		// Tags
		echo "</pre>
<h3>Deleting tags</h3>
<pre>
n";
		$tags = get_terms( 'post_tag', array( 'hide_empty' => false, 'fields' => 'ids' ) );
		if ($tags) {
			foreach($tags as $tag) {
				echo "Tag: $tag
";
				wp_delete_term( $tag, 'post_tag' );

			}
		}
		echo 'I emptied your blog!';
	}
	else {
		echo "</pre>
<h2>Empty Blog</h2>
<pre>
n";
		echo '

Pushing the button below will irretrievably empty your blog of pages, posts, attachments, comments, categories, and tags. Don't push it unless you really mean it!

' . "n";
		echo '</pre>
<form method="post" onsubmit="return confirm('Are you sure?')">';
 echo '<input id="empty" type="submit" name="empty" value="Empty Blog" />';
 echo '</form>
<pre>
';
	}
}

function delete_posts( $options ) {
	$posts = get_posts( $options );
	$offset = 0;
	while( count( $posts ) > 0 ) {
		if( $offset == 10 ) {
			echo "Bailing
n";
			break;
		}
		$offset++;

		echo "</pre>
<h3>Cycle $offset</h3>
<pre>
n";
		foreach( $posts as $post ) {
			print "Post ID: {$post->ID}
n";
			wp_delete_post( $post->ID, true );
		}
		$posts = get_posts( $options );
	}
}

?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s