A couple days ago I started thinking of revitalizing my Tumblr blog into something other than a link sharing farm. But if I was going to do this I wanted to get rid of all of that content first. Tumblr provides a “mass editor” but you have to check each item manually and if you have several thousand entries this isn’t feasible in a short amount of time. I also have two other Tumblr blogs, but this is main one. So I can’t delete it altogether without deleting my other blogs. I searched the interwebs to see if someone had written up something already, but to be honest I couldn’t find anything that actually worked.
I found some python libraries to simplify the task though. The main two worth mentioning that you’ll need are oauth2 and pyblr. After installing these, I then registered for an API key. Don’t think too hard about it. Most fields aren’t required when you’re registering an application. Make sure you request xAuth to be enabled because this is a “desktop application”. Then you can check out my python script I wrote to do the dirty work. Easy peasy! Enjoy your freshly cleaned Tumblr blog.
Here’s the python code:
import pyblr
import oauth2 as oauth
import urllib
import urlparse
import time
'''
!WITH PROPER CREDENTIALS THIS WILL ERASE ALL POSTS FROM THE GIVEN TUMBLR BLOG!
Use with caution and make sure you know what you're doing. That said this is
probably the easiest way to delete all of your posts if you have thousands of
posts. If you have less than that it might be easier for you to just use the
mass editor in the Tumblr UI.
This requires you to get an API key from Tumblr and request xAuth access to
be enabled.
'''
consumer_key = "YOUR_CONSUMER_KEY"
secret_key = "YOUR_SECRET_KEY"
username = "YOUR_EMAIL"
password = "YOUR_PASSWORD"
blog_name = "YOUR_BLOG.tumblr.com"
xauth_access_token_url = "https://www.tumblr.com/oauth/access_token"
request_token_url = "http://www.tumblr.com/oauth/request_token"
authorize_url = "http://www.tumblr.com/oauth/authorize"
access_token_url = "http://www.tumblr.com/oauth/access_token"
consumer = oauth.Consumer(consumer_key,secret_key)
client = oauth.Client(consumer)
client.add_credentials(username,password)
client.authorizations
params = {}
params["x_auth_username"] = username
params["x_auth_password"] = password
params["x_auth_mode"] = 'client_auth'
client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1()
resp, content = client.request(xauth_access_token_url, method="POST", body=urllib.urlencode(params))
if resp['status'] != '200':
raise Exception("Invalid response %s. => %s" % (resp['status'], content))
token_dict = dict(urlparse.parse_qsl(content))
access_token = oauth.Token(token_dict["oauth_token"], token_dict["oauth_token_secret"])
pyblr_client = pyblr.Pyblr(oauth.Client(consumer, access_token))
counter = 0
while True:
try:
posts = pyblr_client.posts(blog_name)
if len(posts) == 0: break
for p in posts["posts"]:
pyblr_client.delete_post(blog_name, params={ "id" : p["id"] })
counter += 1
except ValueError, err:
print "ERROR: %s" % (str(err))
print "sleeping for 5 seconds before retrying"
time.sleep(5)
print "Finished clearing blog: %s" % (blog_name)
print "Deleted %d posts" % (counter)
Hi, I’m new to Python. How do I install the pyblr module to use with this script on a Mac?
Thanks!
Easiest way would be to just copy https://github.com/ToQoz/Pyblr/blob/master/pyblr/pyblr.py to the same directory as the main script.
Thanks for the response. I figured it out. The problem wasn’t at all with the pyblr script, but rather using the most recent version of httplib2. There’s some kind of issue with it and the certificate that tumblr uses. Anyhow, thanks for the script! It’s going to save me a ton of time.
How long did it take you to get xAuth access? I tried your code but I’m getting “Missing or invalid request token,” which I’m assuming is because I don’t have xAuth yet.
I can’t remember but I think Tumblr got back to me within 24-48 hours.
I don’t understand; you don’t give much description. where do we put the code,(on the regular place we put the theme code?) what do we do when we register & what does that do? do we just install those two programs & it’ll be done? i don’t see any install buttons all i see is when i scroll down a code.
i’m lost & don’t know what any of that means. o.O
help?
Sophia, This code runs on your local machine using the Python programming language. It won’t do anything if you paste it into the theme other than probably break the theme. My advice is, if you don’t understand what this code is doing, I wouldn’t attempt to use it. You might try contacting Tumblr support to see if they would help out and wipe your posts for you. It’s probably a long shot, but it never hurts to ask.