0

Is there a function (or a predefined set of functions) that will clear all posts, post tags and categories, all custom posts and custom post terms, attachments, menus and customizer settings?

So far I know how to delete all theme mods with

remove_theme_mods()

I found wp_delete_post() but this needs post ID I guess, since when I called it empty nothing happened.

I need a function that will quickly delete or trash all of the above, because I'm working on an import, so every time I import something I need to go to each page and delete everything manually, and this is tedious. It would save me time if I could do it in a single click/refresh.

  • Something like this? – Pieter Goosen Oct 20 '15 at 9:12
  • Note that plugin recommendations are off-topic, you can ask how to do something, but you can't ask for software recommendations here as a question needs to have a definitive answer – Tom J Nowell Oct 20 '15 at 9:21
  • I updated my question to remove plugin recommendations. – dingo_d Oct 20 '15 at 9:29
  • @PieterGoosen I'd rather not mess with the database tables manually, if I delete something by accident I'd need to do everything from scratch :\ – dingo_d Oct 20 '15 at 9:30
0

So I couldn't find anything so I created this. The only drawback is that it will delete the 'Uncategorized' term in the Posts, but I can live with this since it's just for development purpose.

function delete_all(){
    global $wpdb;
    $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
    $all_post_types = get_post_types();
    $all_taxonomies = get_taxonomies();
    $all_options = wp_load_alloptions();

    foreach ($all_options as $option) {
        delete_option($option);
    }

    foreach ($menus as $menu => $menu_obj) {
        wp_delete_nav_menu($menu_obj->term_id);
    }

    foreach ($all_post_types as $post_type => $post_type_value) {
        $temp_posts = get_posts( array('post_type' => $post_type, 'posts_per_page' => -1));

        foreach ($temp_posts as $temp_post => $temp_post_value) {
            wp_delete_post($temp_post_value->ID);
        }
    }

    remove_theme_mods();

    foreach ( $all_taxonomies as $taxonomy ) {
        // Prepare & excecute SQL - also deletes Uncategorized
        $wpdb->get_results( $wpdb->prepare( "DELETE t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('%s')", $taxonomy ) );

    }

}

add_action('wp_loaded', 'delete_all');

The taxonomy part I found here: http://wpsmith.net/2014/plugin-uninstall-delete-terms-taxonomies-wordpress-database/

I looked at wp_delete_term, but I coulnd't get it to work.

| improve this answer | |
  • 1
    I would recommend adding a big warning text with a backup reminder, into the code, since your code snippet might be copied/pasted out of context ;-) – birgire Oct 20 '15 at 11:27
  • I do agree with @birgire here. And it is really really very important to add such notices when you post code that can irreversably completely trash a site – Pieter Goosen Oct 20 '15 at 11:35
  • Oh, this won't go into production, this is on my local server, because I'm testing a plugin functionality, so I just need to delete everything when I need it. But yeah, I do agree with you both, if I'll post this on github or somewhere I'll make sure to add multiple warnings :) – dingo_d Oct 20 '15 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.