Mastodon Tech Guide

Glitch

We run the "Glitch" version of Mastodon:
https://glitch-soc.github.io/docs/

Delete Accounts

You might want to delete old accounts (older than 30 days) that has NOT make any post.

Sample shell script

#!/bin/bash

# Needs `jq` and `date` installed on system

# Mastodon instance URL and access token
INSTANCE_URL="https://your-instance-url"
ACCESS_TOKEN="your-access-token"

# Function to make API requests
mastodon_api_request() {
    local endpoint="$1"
    local method="${2:-GET}"
    local data="$3"
    
    if [ "$method" = "GET" ]; then
        curl -s -X "$method" -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" "$INSTANCE_URL$endpoint"
    else
        curl -s -X "$method" -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" -d "$data" "$INSTANCE_URL$endpoint"
    fi
    
    sleep 2
}

# Function to get all accounts
get_all_accounts() {
    local page=1
    while :; do
        response=$(mastodon_api_request "/api/v1/admin/accounts?limit=50&page=$page")
        accounts=$(echo "$response" | jq '.[] | @base64')
        if [ -z "$accounts" ]; then
            break
        fi
        echo "$accounts"
        page=$((page + 1))
    done
}

# Function to check if an account has posts
has_posts() {
    local account_id="$1"
    response=$(mastodon_api_request "/api/v1/accounts/$account_id/statuses?limit=1")
    [ "$(echo "$response" | jq 'length')" -gt 0 ]
}

# Function to check if an account is older than 30 days
is_older_than_30_days() {
    local created_at="$1"
    local created_at_date=$(date -d "$created_at" +%s)
    local current_date=$(date +%s)
    local days_old=$(( (current_date - created_at_date) / 86400 ))

    [ "$days_old" -gt 30 ]
}

# Function to remove an account
remove_account() {
    local account_id="$1"
    mastodon_api_request "/api/v1/admin/accounts/$account_id" "DELETE"
}

# Main script
for account in $(get_all_accounts); do
    account=$(echo "$account" | base64 --decode)
    account_id=$(echo "$account" | jq -r '.id')
    created_at=$(echo "$account" | jq -r '.created_at')
    
    if ! has_posts "$account_id" && is_older_than_30_days "$created_at"; then
        result=$(remove_account "$account_id")
        if [ -z "$result" ]; then
            echo "Removed account ID: $account_id"
        else
            echo "Failed to remove account ID: $account_id"
        fi
    fi
done