Delete Unnecessary Post Metadata

By default every time you write or update any Post or Page in WordPress, it creates two very distinctive Post metadata. One of them is _edit_lock and another one is _edit_last. Not to mention they are created for a very specific reason. The “_edit_lock” metadata helps users to have control over their post during the time of editing. It is fairly useful when you have multiple users in one site. It prevents another user from taking over the control or editing capability from the original user. On the other hand “_edit_last” basically stores the time when it was last edited. So far so good.

The problem is, for every single post or page these two post metadata gets stored on your database permanently. It also means, if you have 100 post written on your site, most probably you would have 200 post meta entries that are pretty much useless yet occupying your database space. Over time, these data will pile up on your database and potentially slow down your database query. Another issue is, if you rename the post title of your old post, it will also create another row on your wp_postmeta (generally) table which is known as _wp_old_slug meta key.

WordPress Post Meta TableScreenshot of a WordPress Post Meta Table.

Now all these post meta data is basically useless. However, there are ways to get rid of them. You can either delete them manually from the table or use a plugin to get your job done. Interestingly, I figured out a way to get rid of them with different approach. I wanted to have these meta data stored while I am working and then delete them as I log out from the system. Here is your magic function that you may have been looking for.

function delete_useless_post_meta() {
   global $wpdb;
   $table = $wpdb->prefix.'postmeta';
   $wpdb->delete ($table, array('meta_key' => '_edit_last'));
   $wpdb->delete ($table, array('meta_key' => '_edit_lock'));
   $wpdb->delete ($table, array('meta_key' => '_wp_old_slug')); }

All you need to do now, is to copy this snippet and paste it on your theme’s functions.php page. Now you can do what you do best and stop worrying about your database space. This snippet actually could be your life saver if you are building a large database or if you have large number of entries.


I created a simple function which has been hooked with add_action function and this function would only be called while user is signing out from the system. Within the function, I called for $wpdb object as I intended to delete certain row from the “wp_postmeta” table on your database. Unless you make any change to your table prefix during WordPress installation, your default post meta table should be “wp_postmeta”. However, in my case it wasn’t the default one. In order to avoid possible error, I created $table variable that will retrieve the prefix of your post meta table automatically and hold it on the memory. Then performs “deletion” process from the table that holds specific “meta_key”. This is as simple as it can get.

Note: Please back up your database before you start using this snippet. I personally tested this function on my computer and on my web server and performed without any issue. I do not expect any hiccup in your case either. I hope you would find this post to be useful. Feel free to try out this snippet and let me know if you have any question regarding this issue.

Today In History




  1. NataliiaPublished: 4 years ago

    Unfortunately, this code didn’t delete _edit_lock and _edit_last on my site.
    Maybe my table name, for example, “tablepostmeta”, without “_”?
    And do I need change wp_logout to myprefix_logout?

    Thank you!

    • Iftekhar BhuiyanPublished: 4 years ago

      Hello Nataliia, It really does not matter what prefix you have on your database table name. Because I used

      $table = $wpdb->prefix.'postmeta';

      on my function to deal with this issue. It will automatically grab your table’s prefix and delete the values of the specified meta key. These meta keys gets deleted as you log out from the system. You can check them from PhpMyAdmin as you log out. However, they gets created once again as you edit any post but like always, it gets deleted as you log out. It’s a continuous process.

      You must not change “wp_logout” with anything here because it has nothing to do with your database prefix. Hope that helps. Thank you.”

  2. DeePublished: 4 years ago

    rare sharing indeed. thanks

    • Iftekhar BhuiyanPublished: 4 years ago

      You are welcome 🙂

  3. AnnPublished: 4 years ago

    Forgive my complete ignorance, but if I add this to my child theme’s functions.php, will I still see post meta data on my wordpress posts? Thank you!

    • Iftekhar BhuiyanPublished: 4 years ago

      Dear Ann, I never tested this code on any “child theme”. However, I don’t see any reason why it won’t work. Then again I would highly recommend you to use this on the “parent theme” if you can. As far as post “meta data” is concern, this code will delete only few meta data (defined by meta key i.e. ‘_edit_last’) and rest (if there is any) should remain as it is. Hope that helps. Thank you.

  4. David GreenwallPublished: 4 years ago

    This is very helpful! Thank you for sharing.

    • Iftekhar BhuiyanPublished: 4 years ago

      Hello David, I am happy to know that you find this snippet to be useful. You are welcome and thank you for stopping by.

  5. Dipika DubeyPublished: 7 years ago

    Thanks a lot……explained very well.

    • Iftekhar BhuiyanPublished: 7 years ago

      You are welcome Dipika, I am glad that it was helpful for you 🙂

  6. Marcus Published: 7 years ago

    Great Answer, Thanks a lot!

  7. MarcusPublished: 7 years ago

    Thank you so much!

    It works great now! i am adding all kinds of stuff for it to delete. Is there a way to see which meta_key there is most rows of?
    I see that some have values, but I dont know if I need them i.e:
    – _downloadable (no) – I don’t have downloadable products
    – _virtual (no) – I am not using virtual products
    – _stock_status (yes) – I do not use stock at all cus we do drop shipping

    Are they safe to take away?
    Thank you!

    • Iftekhar BhuiyanPublished: 7 years ago

      It seems like you are using some sort of eCommerce plugin (WooCommerce most probably). In that case, I won’t recommend you to delete them. They are used and most probably required to store product meta data. If you really want to delete them, please do it at your own risk. You can use my function regardless to delete any meta key and their value. Hope that helps 🙂

  8. MarcusPublished: 7 years ago

    Hello, I tried to put it in functions but got a blank screen for the webpage. What might I have done wrong? Is it outdated? You are right! This is exactly what I have been looking for! Is there something I could do to remove them by phpMyadmin by query instead of php. I have 500 000 rows… Thanks for the post!

    • Iftekhar BhuiyanPublished: 7 years ago

      Marcus, the only problem I see here is the name of my function (delete_post_meta) itself. I figured, that it is also the name of a native WP function. Perhaps, that could be an issue. However, I updated the snippet with new function name and changed it to “delete_useless_post_meta”. Also, please remember that you shouldn’t use “<?php” and “?>” tag while copying this snippet on your theme’s functions.php page. Can you try with this new snippet and let me know? Thanks.

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.