10 useful SQL Queries To Clean Up Your WordPress Database

10 useful SQL Queries To Clean Up Your WordPress Database

While working on WP is a great experience, however, all your work piles up and in no time, it can become a hefty platform. To save myself from this dreaded situation I use certain SQL queries which keep my WP squeaky clean! Those are:

  • Delete all the revisions: Using DELETE FROM `wp_posts` WHERE `post_type`="revision" query you can clear all your previous work revisions from the database.
  • Remove those trash: All of your work that you have removed goes to the trash section in the database. It stays there forever. To get rid of them completely use DELETE FROM `wp_posts` WHERE `post_status`="trash". This will save you a lot of time if you have many posts.
  • Why keep those spam comments? There is a funny meme that states “The internet is dark and full of spam”. It couldn’t have been more apt for spam mails and WordPress. 

IF you want to get rid of the spam from your WP, just type DELETE FROM wp_comments WHERE comment_approved = 'spam'. 

  • Let the transients go: Yes, for Wp users transients are super useful, as they store those kind of data that takes a long while to be found. But transients hold a lot of your database, especially when you are not managing it properly.

So use DELETE FROM `wp_options` WHERE `option_name` LIKE ('%\_transient\_%') to have some extra space in your database.

  • Ping the pingbacks bye: DELETE FROM `wp_comments` WHERE `comment_type` = 'pingback' to delete all the pingbacks from the comments section.
  • Clean the metadata: Type the below query:

DELETE m FROM `wp_postmeta` AS m

LEFT JOIN `wp_posts` AS p ON m.`post_id` = p.`ID

WHERE p.`ID` IS NULL

This little query will delete all the metadata that aren’t connected with any post.

  • From keywords to plugins, say bye to it all: DELETE FROM `wp_postmeta` WHERE `meta_key` LIKE '%aktt%'. Using this, clean all the metadata, and the keywords. It is also very handy to remove a common plugin meta, using that particular keyword.
  • Sayonara to shortcodes: If you are like me and have tested and tried many plugins, then this query is just for you. All you need to do is type UPDATE wp_post SET post_content = replace(post_content, '[your-shortcode]', '' ) and you are devoid of all the shortcodes that you didn’t even know existed!
  • For the admins! If you are one of those blog admins who are tired of bulk, unapproved admins, then use DELETE from wp_comments WHERE comment_approved = '0'. Within seconds all the unwanted, not approved comments will be gone, just like Harry Potter would have done!
  • Update that maid ID: Inside the WP database tables all the data are stored. So, you can use a simple SQL query to update your admin mail ID. Write:

UPDATE `wp_users` SET `user_email` = "new_email_address" WHERE `wp_users`.`user_login` = "admin". This will help you to update any mail Id within the wp_users` table.

While writing this post did I realise, my own WP was cluttered and this post helped me to clear a lot of it. I am not bragging, but saying these queries are gems in themselves. For a developer it is important his/ her platform is crisp, impeccable so that the codes are crisp and spotless themselves.

#WordPress #WordPressDatabase #SQLQueries #WordPressTipsandTricks

Try them yourselves and share your experiences!

Lovingly,

Rina

Kamalakannan Sivanandam

Entrepreneur | Web Development Expert | Empowering Personal Growth & Mentorship | Author

1 年

Thank you, Rina Chhadwa, for sharing these valuable SQL queries to enhance WordPress database cleanliness. I appreciate the manual insights. Moreover, for those who might not be familiar with SQL, plugins like wpOptimize and Advanced Database Cleaner offer user-friendly alternatives. These tools provide additional features such as scheduled database cleaning. Remember to always take a backup before initiating any database cleaning process. Your contribution sheds light on manual approach, offering a comprehensive guide for users with varying levels of technical expertise. Much appreciated!

回复
Omid Moghadasi

Development Team Lead at markazeahan | ???????

1 年

amazing ??

回复
Cees Rijken

Technisch Consultant / Informatie Analist / Functioneel Ontwerper

2 年

Using?just "DELETE FROM `wp_posts` WHERE `post_type`="revision"" could still leave orphaned metadata. To be more thorough: DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id ) LEFT JOIN wp_term_taxonomy d ON ( b.term_taxonomy_id = d.term_taxonomy_id) WHERE a.post_type = 'revision' AND d.taxonomy != 'link_category'

回复

要查看或添加评论,请登录

Rina Chhadwa的更多文章

  • Checklist for launching a website

    Checklist for launching a website

    It is no rocket science that launching a website is a mammoth task. Not only do you need to keep a tab of the work you…

  • How to choose the right platform for your business?

    How to choose the right platform for your business?

    There are a few platforms which you can choose while you build your/ anyone’s website for the first time. Choosing a…

  • 10 common WordPress theme mistakes and how to prevent them

    10 common WordPress theme mistakes and how to prevent them

    When I talk about WP, the first thing that comes to my mind is WP themes. It is like an exquisite collection of a…

    4 条评论
  • Are you a cafe owner? 5 things to know before designing your website

    Are you a cafe owner? 5 things to know before designing your website

    Owning a cafe great. There are few cafes that are my absolute favourite.

  • 15 things I learned in my 15 years of career

    15 things I learned in my 15 years of career

    In the work community, we always talk about the value that experience brings. It is one of the main reasons why…

    1 条评论
  • Why using a Framework is a great idea?

    Why using a Framework is a great idea?

    Hello folks! How have you been? I had a great time this Independence Day (you peeps saw the pic, didn’t you?) Today…

  • 10 Web Development Definitions You Need to Know

    10 Web Development Definitions You Need to Know

    #WebDevelopment is the process of building, creating and managing websites. This process includes web design, web…

  • 1 Drupal tip I live by

    1 Drupal tip I live by

    David Wheeler once said’ “All problems in computer science can be solved by another level of indirection.” When it…

  • 5 Time management techniques for Mompreneurs

    5 Time management techniques for Mompreneurs

    How many women you know who had to leave their jobs to be a full time mom? Yes, the numbers are high. Being a mom is…

    3 条评论
  • Happy Independence Day!!

    Happy Independence Day!!

    #HappyIndependenceDay guys! It's been 72 years that our motherland has been independent. We have come a long way since…

社区洞察

其他会员也浏览了