I am creating a plugin in wordpress and the thing I am trying to do is to create pages with code using wp_insert_post.
After creating each page and removing all of them the new page with the same slug is being created with this format slug-number.

My question is how can I remove the page entirely from wordpress and database so each time a page is being created the previous one be deleted completely and the new ones slug become just the slug I provide without any number attached at the end of slug?

  • have you try to delete definitively the old post before to create the new one ? another try would be to change the slug of the old post before to create the new one
    – mmm
    Commented Sep 6, 2016 at 8:28

1 Answer 1


You must use wp_delete_post() with second parameter set to true. The post will be completely deleted, not trashed.

// Use $args to get the pages you created previously
// See https://developer.wordpress.org/reference/functions/get_pages/
$pages = get_pages( $args );

if( $pages ) {
    foreach( $pages as $page ) {
        wp_delete_post( $page->ID, true );

After that, you can use wp_insert_post() to create a new post with a slug previously used.

It may worth to consider to update the post instead of delete and create a new one.

  • It does remove the post entirely but the slug still adds a number to the end. I want the slug to reset too Commented Sep 6, 2016 at 8:55
  • You need to create the new post after removing the previous one. Tested and working here.
    – cybmeta
    Commented Sep 6, 2016 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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