最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

scripting - How to remove all FlagSmith identities at once - Stack Overflow

programmeradmin3浏览0评论

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.

How could we delete all of those?

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

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.

How could we delete all of those?

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

Share Improve this question asked 18 hours ago Soldeplata SaketosSoldeplata Saketos 3,4611 gold badge27 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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/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!

发布评论

评论列表(0)

  1. 暂无评论