Delete all your reddit comments

I noticed that my reddit account had a few posts that were too revealing. So, I decided to remove them. I stumbled upon a couple greasemonkey scripts to delete all posts, which got my interested in seeing if I could write up a quick script to do the same that could run in the Developer Tools of any browser, rather than relying on an extension.

The catch is that you, apparently, should edit each comment’s content to remove it before deleting it, otherwise it’s still cached in other users' inboxes or something. Another catch is that reddit’s servers don’t like lots of simultaneous requests.

Still, it turned out easier than I expected:

var total = $('div#siteTable div.comment:not(.hidden)').length;
$('div#siteTable div.comment:not(.hidden)').each(function (i) {
  var that = this;
  setTimeout(function () {
    if ($(that).find('textarea').text() != ".") {
      $(that).find('textarea').text('.');
      $(that).find('div.usertext-buttons button.save').click();
    }

    setTimeout(function () {
      $(that).find('form.del-button a.yes').click();
    }, (total + i) * 1100);
  }, i * 1100);
});

It runs from the user’s comments page. First it gathers all comment divs that aren’t hidden. Hidden means we’ve probably already deleted it. Then, for each one of those, we do a few things after a delay:

  1. If the text isn’t already a “.”, edit it and save it.
  2. After another delay, click delete’s “yes” link.

Unlike the other scripts, It doesn’t bother clicking the edit or delete links since those just toggle visiblity of other components. The delays are multiplied by the index of each comment, to spread the server requests out over time. If you have 300 comments, this will take around 600 seconds. Note that the delay is slightly larger than a second to help ensure requests are always at least a second apart. I had some issues with rejected requests leaving it at exactly 1000 ms, due to the actual delay being slightly less I’m sure.

Without the RES extension and “Never ending reddit” enabled this will take a while. With it, you need to hold down the “End” key until all your comments are loaded at once. Or you can do it in increments. So, there’s still some manual labor involved, but considerably less. It worked well enough for my few comments.

Update:

Apparently this is a popular thing. As per request, a slightly fancier updated version that doesn’t have such a naive delay, and outputs progress:

var total = $('div#siteTable div.comment:not(.hidden)').length;
$('div#siteTable div.comment:not(.hidden)').each(function (i) {
  var that = this;
  setTimeout(function () {
    var flag = false;
    if ($(that).find('textarea').text() != ".") {
      $(that).find('textarea').text('.');
      $(that).find('div.usertext-buttons button.save').click();
      flag = true;
    }

    function del() {
      $(that).find('form.del-button a.yes').click();
      console.debug("Deleted " + i + "/" + total);
    }

    if (flag) setTimeout(del, (total + i) * 1100);
    else del();
  }, i * 1100);
});