Prune old statuses

I am running a gotosocial instance for my personal experiments. One of the bots I am running there is stuendlich@cress.space which toots the current time every hour to give a feeling for time in my timeline.

I wanted to prune the old toots of this bot, because they are useless when older than a few days. Mastodon has a feature to prune older statuses, but gotosocial is missing this currently. There is a ready to use Python script to use for Mastodon named ephemetoot. But this fails for gotosocial because the version reported is not bigger than 1.0 (which would be the Mastodon version needed). So I needed to write code against the api myself (again).

First getting the account id of the current logged in user:

r = requests.get(
    url="https://fedi.cress.space/api/v1/accounts/verify_credentials",
    headers=headers,
)
account_id = r.json()["id"]

Then get the next 30 (gotosocial default) statuses:

max_id = None
r = requests.get(
    url=f"https://fedi.cress.space/api/v1/accounts/{account_id}/statuses",
    headers=headers,
    params={"max_id": max_id},
)

The max_id has to be set to the previously last status to get the next 30 statuses. This is the way the Mastodon API is doing pagination.

And finally deleting a status:

r = requests.delete(
    url=f"https://fedi.cress.space/api/v1/statuses/{status_id}",
    headers=headers,
)
assert r.status_code == 200

The status code should be 200. For ratelimiting the code is 429.

Deleting a lot of statuses obviously results in a rate limit. Which is good! So maybe more than one run (after waiting for 15 minutes) is needed to prune all old toots. The script breaks (on purpose) when a rate limit happens.

The full code of the script is in the stuendlich-bot github repo.

The code is running in a Github Action once a day.