#!/usr/bin/env python
#    PREFACE
# If you want to keep a copy of your data, request it here (takes a few days):
#              https://www.reddit.com/settings/data-request
# with this, you can delete ALL your comments by putting comments.csv from the request
# into the directory with the script. Warning: it does NOT ask you.
# Without the file, the script edits 100 comments to voice the protest and
# propagate itself, then deletes what it can (~1000 comments).
#    SETUP
# Save this script as `unreddit.py`
# Install Python, any version 3.7+.
# Run `pip install praw` in terminal--this package accesses Reddit API.
# Go to https://www.reddit.com/prefs/apps to create an app.
# Name: anything, I called it "unexister" as it can't contain "reddit".
# Type: `script`. Redirect url: `http://localhost`.
# After creation, you will find:
# CLIENTID under "personal use script", and
# CLIENTSECRET by "secret".
# REDDITUSERNAME and REDDITPASSWORD you already know.
# Input them all in the variables below these instructions.
#    EXECUTION
# Execute this file with `python unreddit.py` on any platform.
# On Linux (e.g. RasPi), you may want to use `nohup python unreddit.py > unreddit.log &` 
# to let it run in the background until finished, as it can take a LONG while.

CLIENTID = ''
CLIENTSECRET = ''
REDDITUSERNAME = ''
REDDITPASSWORD = ''
COMMENTSCSV = 'comments.csv'

################### here be dragons ###################

import os
import praw
from datetime import datetime

USERAGENTNAME = f'void:unexister:1 (by u/{REDDITUSERNAME})'

myreddit = praw.Reddit(
    client_id     = CLIENTID,
    client_secret = CLIENTSECRET,
    user_agent    = USERAGENTNAME,
    username      = REDDITUSERNAME,
    password      = REDDITPASSWORD
    )

myreddit.validate_on_submit = True
user = myreddit.redditor(REDDITUSERNAME)

# we'll replace comments with this text.
replace_with = """#!> This comment has been edited in protest to reddit's decision to bully 3rd party apps into closure.
If you want to do the same, you can find instructions here:
https://rentry.co/unreddit
"""


# delete ALL comments from the csv file
if COMMENTSCSV and os.path.exists(COMMENTSCSV):
    import csv
    with open(COMMENTSCSV, 'r', encoding='utf-8-sig') as f:
        reader = csv.reader(f)
        headers = next(reader)
        content = []
        for row in reader:
            content.append(dict(zip(headers, row)))

    for c in content:
        # print some info about the comment
        text = f"{c['subreddit']} {c['body']}"[:60].replace('\n', '|')
        print('\n', c['date'], c['id'], text, end='')
        # there's no fast way (w/o fetching) to check if already gone,
        # so don't restart this. leave it churning until it's done
        myreddit.comment(id=c['id']).delete()
        print(" — deleted", end='')
# otherwise just do your best
else:
    count = 0
    for comment in user.comments.new(limit=None):
        count +=1
        # print some info about the comment
        time = datetime.fromtimestamp(comment.created_utc).isoformat()
        text = f"{comment.subreddit.display_name} {comment.body}"[:60].replace('\n', '|')
        print('\n', time, comment.id, text, end='')

        # if we've already edited this comment, move to the next one
        if "#!>" in comment.body:
            continue

        # edit 100 comments, delete the rest
        if count >= 100:
            comment.delete()
            print(" — deleted", end='')
        else:
            comment.edit(replace_with)
            print(" — edited", end='')
Edit
Pub: 10 Jun 2023 07:17 UTC
Edit: 20 Jun 2023 18:57 UTC
Views: 592