Delete followers with API

Hello everyone,

I would like to delete automaticly the followers of my CRM contacts with the API. I tried to run a python script but for the moment I can’t design something without errors. If someone already had this problem don’t hesitate to reach me.

Best,

You can try this, using PHP

<?php

// Replace with your Pipedrive API token
$apiToken = 'YOUR_API_TOKEN';

// Pipedrive API base URL
$baseUrl = 'https://api.pipedrive.com/v1/';

// Function to make a DELETE request
function deleteRequest($url)
{
    global $apiToken;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $apiToken,
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

// Function to fetch and delete followers of a person
function deleteFollowers($personId)
{
    global $baseUrl;
    $followersUrl = $baseUrl . "persons/{$personId}/followers";

    $followersResponse = deleteRequest($followersUrl);
    $followersData = json_decode($followersResponse, true);

    if (isset($followersData['data']) && !empty($followersData['data'])) {
        foreach ($followersData['data'] as $follower) {
            $followerId = $follower['id'];
            echo "Deleted follower {$followerId} from person {$personId}\n";
        }
    }
}

// Function to fetch and delete followers of all persons
function deleteAllFollowers()
{
    global $baseUrl;
    $personsUrl = $baseUrl . 'persons';

    $page = 1;
    do {
        $personsResponse = deleteRequest("{$personsUrl}?start={$page}");
        $personsData = json_decode($personsResponse, true);

        if (isset($personsData['data']) && !empty($personsData['data'])) {
            foreach ($personsData['data'] as $person) {
                $personId = $person['id'];
                deleteFollowers($personId);
            }
        }

        $page++;
    } while (!empty($personsData['additional_data']['pagination']['more_items_in_collection']));

    echo "All followers deleted successfully!\n";
}

// Call the function to delete all followers
deleteAllFollowers();

Hi Walid,

Thanks for your anwer. Could you confirm this script works. I tried and I can run the script but that doesn’t delete followers of my contacts on Pipedrive.
image
image

Could you help me ?