{"id":287,"date":"2013-10-24T18:27:48","date_gmt":"2013-10-24T10:27:48","guid":{"rendered":"http:\/\/play.datalude.com\/blog\/?p=287"},"modified":"2013-10-24T18:27:48","modified_gmt":"2013-10-24T10:27:48","slug":"wordpress-multisite-blog-mass-removal-script","status":"publish","type":"post","link":"https:\/\/play.datalude.com\/blog\/2013\/10\/wordpress-multisite-blog-mass-removal-script\/","title":{"rendered":"WordPress Multisite Blog Mass Removal Script"},"content":{"rendered":"<p>I was moving a WPMU site to a new server and saw that the database had grown to about 250Mb, and there were about 700 blogs running on the server. I used a few database queries to pull out a list of which blogs I wanted to delete &#8212; basically any which had a creation date and last update date the same, and any with only one post etc &#8212; at which point I had a list of 300 blog IDs. So was I going to manually delete them all? Nope, I did what any sensible person did, and wrote a script to do it.<\/p>\n<p><!--more-->This one is a bash script. Hope someone else can get some use out of it. Obviously try it on\u00a0 a test database first. You'll need to change the database name, and the MYSQL_OPT line to reflect your user and password, natch, and then fill in your list of blog IDS &#8230;<\/p>\n<p>As always if you copy and paste it you'll need to make sure the double and single quotes are normal, and not curly.<\/p>\n<pre>#!\/bin\/bash\r\n\r\nDATABASE=database_name\r\nLOGFILE=monitor.txt\r\nMYSQL_OPT=\"-u root -ppassword -sN -e \"\r\n\r\n# Clear logfile\r\necho \"\" &gt; $LOGFILE\r\n\r\nfor BLOG_ID in 654 655 656 657 658 659 660 661 663 664 665 666 667 668 669 670\r\n\r\ndo \r\n\u00a0\u00a0 \u00a0echo blog id is $BLOG_ID &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0USER_EMAIL=$(mysql $MYSQL_OPT 'SELECT option_value FROM wp_'\"$BLOG_ID\"'_options WHERE option_name=\"admin_email\" LIMIT 0,1;' $DATABASE)\r\n\u00a0\u00a0 \u00a0echo user email is $USER_EMAIL &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0USER_ID=$(mysql $MYSQL_OPT 'SELECT user_id FROM wp_usermeta WHERE meta_key=\"primary_blog\" and meta_value='\"$BLOG_ID\"' ;' $DATABASE)\r\n\u00a0\u00a0 \u00a0if [ -z \"$USER_ID\" ]\r\n\u00a0\u00a0 \u00a0then\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0echo \"no user id. skipping user delete\"\u00a0 &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0else\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0echo user id is $USER_ID ... deleting user &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DELETE FROM wp_usermeta WHERE user_id='\"$USER_ID\"' ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DELETE FROM wp_users WHERE ID='\"$USER_ID\"' LIMIT 1;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0fi\r\n\u00a0\u00a0 \u00a0echo \"Deleting Blog tables\" &gt;&gt; $LOGFILE \r\n\u00a0\u00a0\u00a0 mysql $MYSQL_OPT 'DELETE FROM wp_blogs WHERE blog_id='\"$BLOG_ID\"' LIMIT 1;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0\u00a0 mysql $MYSQL_OPT 'DELETE FROM wp_blog_versions WHERE blog_id='\"$BLOG_ID\"' LIMIT 1;' $DATABASE &gt;&gt; $LOGFILE\r\n\r\n\u00a0\u00a0\u00a0 mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_commentmeta ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_comments ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_links ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_options ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_postmeta ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_posts ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_terms ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_term_relationships ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0mysql $MYSQL_OPT 'DROP TABLE wp_'\"$BLOG_ID\"'_term_taxonomy ;' $DATABASE &gt;&gt; $LOGFILE\r\n\u00a0\u00a0 \u00a0echo \"------------------\" &gt;&gt; $LOGFILE\r\n\r\ndone\r\n\r\n# Debugging - opens logfile\r\nmore $LOGFILE\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was moving a WPMU site to a new server and saw that the database had grown to about 250Mb, and there were about 700 blogs running on the server. I used a few database queries to pull out a list of which blogs I wanted to delete &#8212; basically any which had a creation &#8230; <a title=\"WordPress Multisite Blog Mass Removal Script\" class=\"read-more\" href=\"https:\/\/play.datalude.com\/blog\/2013\/10\/wordpress-multisite-blog-mass-removal-script\/\" aria-label=\"Read more about WordPress Multisite Blog Mass Removal Script\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,4],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-it","category-linux"],"_links":{"self":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":0,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"wp:attachment":[{"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/play.datalude.com\/blog\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}