One of the most common questions asked is “How can I bulk delete all my categories in my WordPress Site.

Here are the steps you need to take but please foillow every step to avooid disapppointment.

Under no circumstances make these changes before taking a full backup.

Create a Backup: Before making any changes, ensure you have a full backup of your WordPress site not held within your WordPress Filing System. We strongly recommend UpdraftPlus. This is crucial to prevent data loss. 

Step 1: Create the Plugin Folder and File

  1. Create a New Plugin Folder and File (if not already done)
    • Create a new folder in the wp-content/plugins directory, e.g., bulk-delete-categories.
    • Inside this folder, create a file named bulk-delete-categories.php.


Step 2: Add the Code:

<?php
/*
Plugin Name: Bulk Delete Categories with Feedback and Move Posts
Description: A plugin to bulk delete all categories with user feedback and move posts to “Uncategorized”.
Version: 1.2
Author: Your Name
*/

function move_posts_to_uncategorized() {
// Get the ID of the “Uncategorized” category
$uncategorized_id = get_cat_ID(‘Uncategorized’);

// Get all posts in the categories being deleted
$categories = get_categories(array(
‘hide_empty’ => false
));

foreach ($categories as $category) {
if ($category->term_id != $uncategorized_id) {
$posts = get_posts(array(
‘category’ => $category->term_id,
‘numberposts’ => -1
));

foreach ($posts as $post) {
wp_set_post_categories($post->ID, array($uncategorized_id), true);
}
}
}
}

function delete_all_categories_with_feedback() {
// Move posts to “Uncategorized” category
move_posts_to_uncategorized();

// Get all categories and delete them
$categories = get_categories(array(
‘hide_empty’ => false
));

foreach ($categories as $category) {
if ($category->term_id != get_cat_ID(‘Uncategorized’)) {
wp_delete_term($category->term_id, ‘category’);
}
}
}

function bulk_delete_categories_admin_notice($message, $type = ‘success’) {
?>
<div class=”notice notice-<?php echo esc_attr($type); ?> is-dismissible”>
<p><?php echo esc_html($message); ?></p>
</div>
<?php
}

// Add an admin menu item to trigger the deletion
function bulk_delete_categories_menu() {
add_submenu_page(
‘tools.php’, // Parent slug
‘Bulk Delete Categories’, // Page title
‘Bulk Delete Categories’, // Menu title
‘manage_options’, // Capability
‘bulk-delete-categories’, // Menu slug
‘bulk_delete_categories_page’ // Function to display the page content
);
}

function bulk_delete_categories_page() {
if (isset($_POST[‘bulk_delete_categories’])) {
check_admin_referer(‘bulk_delete_categories_nonce’);

// Perform the deletion
delete_all_categories_with_feedback();

// Add an admin notice
bulk_delete_categories_admin_notice(‘All categories have been successfully deleted and posts moved to “Uncategorized”.’);
?>
<div class=”wrap”>
<h1>Bulk Delete Categories</h1>
<p>The categories have been deleted. Please check your categories and posts.</p>
</div>
<?php
} else {
?>
<div class=”wrap”>
<h1>Bulk Delete Categories</h1>
<form method=”post”>
<?php wp_nonce_field(‘bulk_delete_categories_nonce’); ?>
<p>Click the button below to delete all categories and move posts to “Uncategorized”. This process may take some time if you have a lot of categories and posts.</p>
<p>
<input type=”hidden” name=”bulk_delete_categories” value=”true”>
<button type=”submit” class=”button button-primary”>Run</button>
</p>
</form>
</div>
<?php
}
}

add_action(‘admin_menu’, ‘bulk_delete_categories_menu’);

How It Works

  1. Admin Menu Option:

    • The plugin adds a new menu item under Tools named “Bulk Delete Categories”.

  2. Admin Page:

    • When you click on this menu item, you will see a page with a “Run” button to start the category deletion and post move process.

  3. Feedback Messages:

    • When you click the button, it will display a message indicating that the process has started and provide feedback once the process is complete.
    • The process will move all posts to the “Uncategorized” category before deleting all other categories.

  4. Nonce Verification:

    • The script includes nonce verification for security.

How to Use

  1. Install the Plugin:

    • Place the plugin folder bulk-delete-categories in the wp-content/plugins directory.

  2. Activate the Plugin:

    • Go to your WordPress dashboard.
    • Navigate to Plugins > Installed Plugins.
    • Activate the “Bulk Delete Categories with Feedback and Move Posts” plugin.

  3. Run the Deletion Process:

    • Go to Tools > Bulk Delete Categories.
    • Click the “Run” button to delete all categories and move posts to “Uncategorized”.
    • You will see a message indicating that the process has started.
    • Once the process is complete, you will see a notice confirming the deletion and post move.

 

This plugin provides a user-friendly way to delete all categories, move all posts to the “Uncategorized” category, and receive detailed feedback on the process.