Skip to content

Instantly share code, notes, and snippets.

@KatrinaHoffert
Last active June 16, 2019 20:11
Show Gist options
  • Save KatrinaHoffert/0e30986b88d1921312c2ce1a4e630214 to your computer and use it in GitHub Desktop.
Save KatrinaHoffert/0e30986b88d1921312c2ce1a4e630214 to your computer and use it in GitHub Desktop.
Mass deletes reddit comments by URL, editing them first, waiting, then removing for real
import praw
import sys
import time
# Setup
# 1. Install Python 3.6+ <https://www.python.org/downloads/> and PRAW
# <https://praw.readthedocs.io/en/latest/getting_started/installation.html>.
# 2. Create a script here <https://www.reddit.com/prefs/apps/> and
# set the script ID and secret below, as well as your username and
# password.
# 3. Fill in comments_to_remove.
# 4. Run as `python3 mass_delete.py`.
reddit = praw.Reddit(client_id='<FILL ME IN>',
client_secret='<FILL ME IN>',
username='<FILL ME IN>',
password='<FILL ME IN>',
user_agent='MassDelete script using PRAW')
# Direct URLs to comments.
comments_to_remove = [
# FILL ME IN (eg:)
'https://www.reddit.com/r/politics/comments/bwpqqi/we_are_us_senator_ron_wyden_and_reddit_ceo_steve/epzk051/',
'https://www.reddit.com/r/politics/comments/bwpqqi/we_are_us_senator_ron_wyden_and_reddit_ceo_steve/epzgwxb/',
]
print(f'Editing {len(comments_to_remove)} comments.')
def progress_bar(percent, new):
"""Prints a progress bar of equal signs.
Percent is in [0, 1]. If new, will not print newline and will overwrite
existing line."""
bar_width = 50
num_equal_signs = int(percent * bar_width)
s = ''
if not new:
s += '\r'
s += '['
s += '=' * num_equal_signs + ' ' * (50 - num_equal_signs)
s += ']'
print(s, end='', flush=True)
progress_bar(percent=0.0, new=True)
non_existing = []
errors = []
edited_urls = []
for i, comment_url in enumerate(comments_to_remove):
comment = reddit.comment(url=comment_url)
if comment is None:
non_existing.append(comment_url)
else:
try:
comment.edit('[cleared for privacy]')
edited_urls.append(comment_url)
except Exception as e:
errors.append(f'Couldn\'t edit {comment_url}: {e}')
progress_bar(percent=float(i + 1) / len(comments_to_remove), new=False)
print() # progress_bar doesn't print a newline.
if non_existing:
print('The following URLs do not exist:')
print('\n'.join(non_existing))
if errors:
print('Errors encountered:')
print('\n'.join(errors))
if not edited_urls:
print('No comments edited. Exiting.')
sys.exit(1)
print(f'Deleting {len(edited_urls)} comments.')
progress_bar(percent=0.0, new=True)
num_deleted = 0
non_existing = []
errors = []
for i, comment_url in enumerate(edited_urls):
comment = reddit.comment(url=comment_url)
if comment is None:
non_existing.append(comment_url)
else:
try:
comment.delete()
num_deleted += 1
except e:
errors.append(f'Couldn\'t delete {comment_url}: {e}')
progress_bar(percent=float(i + 1) / len(edited_urls), new=False)
print()
if non_existing:
print('The following URLs do not exist:')
print('\n'.join(non_existing))
if errors:
print('Errors encountered:')
print('\n'.join(errors))
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment