Soldeplata Saketos
Soldeplata Saketos

Reputation: 3461

How to remove all FlagSmith identities at once

When using the FlagSmith User Interface, I couldn't find any way to select all the identities and delete them at once.

As you can see in the image attached, we are forced to click the trash icon individually. Identities interface of Flagsmith does not allow for multiple selection

How could we delete all of those?

Clicking every single trash icon would be unbearable if we have more than 20 identities stored

Upvotes: 0

Views: 14

Answers (1)

Soldeplata Saketos
Soldeplata Saketos

Reputation: 3461

By creating a Node.js script to get all the identities in the selected environment (in chunks of 100 items as it's the maximum the API provides) and then delete them one by one:

// fileName: deleteFlagSmithIdentities.js
const axios = require('axios');
// Configuration
const FLAGSMITH_API_URL = process.env.FLAGSMITH_API_URL || 'https://api.flagsmith.com/api/v1'; // Update if using self-hosted instance
const API_TOKEN = process.env.API_TOKEN // Admin API token (global for FlagSmith)
const PROJECT_ID = process.env.PROJECT_ID // Client-side Environment Key
const BATCH_SIZE = process.env.BATCH_SIZE || 100; // Number of identities to fetch per request

// Axios instance with authentication
const apiClient = axios.create({
    baseURL: FLAGSMITH_API_URL,
    headers: {
        maxBodyLength: Infinity,
        Authorization: `Api-Key ${API_TOKEN}`,
        "Content-Type": "application/json",
    },
});

// Function to get all identities
async function getIdentities() {
    try {
        const response = await apiClient.get(`/environments/${PROJECT_ID}/edge-identities/?page_size=${BATCH_SIZE}`);
        return response.data;
    } catch (error) {
        console.error(error);
        console.error("Error fetching identities:", error.response?.data || error.message);
        return null;
    }
}

// Function to delete an identity
async function deleteIdentity(identityId) {
    try {
        await apiClient.delete(`/environments/${PROJECT_ID}/edge-identities/${identityId}`);
        console.log(`Deleted identity: ${identityId}`);
    } catch (error) {
        console.error(error);
        console.error(`Error deleting identity ${identityId}:`, error.response?.data || error.message);
    }
}

// Main function to remove all identities
async function removeAllIdentities() {
    console.log("Fetching identities...");
    const data = await getIdentities();
    const size = data?.results?.length;
    console.log('\x1b[31m%s\x1b[0m', `Size: ${size}`); // red log


    await Promise.all(data.results.map(({ identity_uuid, identifier }) => {
        console.log('\x1b[36m%s\x1b[0m', `deleting ${identifier}`); // cyan log
        return deleteIdentity(identity_uuid);
    }))

    // as we don't have pagination, we just re-run until we get zero identities
    if (size > 0) return await removeAllIdentities()
    console.log("All identities removed.");
}

// Run the script
removeAllIdentities().catch(console.error);

and then run:

node deleteFlagSmithIdentities.js

Remember to add your environmental variables!

Upvotes: 0

Related Questions