0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Qiitaのストック全消し用スクリプト

Last updated at Posted at 2021-12-05

概要

過去のQiita記事のストックを全部消そうとしましたが、
UIからでは1件ずつしか消せなかったので、スクリプトでまとめて消したときのメモです。

注意事項

このスクリプトを実行すると、記事のストックがすべて削除されます。

使い方

環境変数に下記をセットしてから、スクリプトを実行する。

export QIITA_TOKEN=[APIアクセストークン]
export USER_NAME=[ユーザ名]

スクリプト

記事ストックのリストを取得し、それぞれに対して削除のリクエストを発行します。

import os
import json
import requests


def get_next_url(response):
    link = response.headers['link']
    if link is None:
        return None

    links = link.split(',')
    for link in links:
        if 'rel="next"' in link:
            return link[link.find('<') + 1:link.find('>')]

    return None


def get_items(token, user_name):
    url = 'https://qiita.com/api/v2/users/{}/stocks'.format(user_name)
    headers = {'Authorization': 'Bearer {}'.format(token)}

    items = []
    while True:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        items.extend(json.loads(response.text))
        url = get_next_url(response)
        if url is None:
            break

    return items


def delete_item(token, item_id):
    url = 'https://qiita.com/api/v2/items/{}/stock'.format(item_id)
    headers = {'Authorization': 'Bearer {}'.format(token)}
    response = requests.delete(url, headers=headers)


def main():
    token = os.environ['QIITA_TOKEN']
    user_name = os.environ['USER_NAME']
    items = get_items(token, user_name)

    for item in items:
        delete_item(token, item['id'])


if __name__ == '__main__':
    main()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?