Mass Deleting Records In Zoho CRM using the API

Mass Deleting Records In Zoho CRM using the API

Pre-requisites

  1. Using Node.js  
  2. In project folder install Axios (npm install axios)
  3. In project folder, install dotenv to use .env
  4. .create .env file

.env File Structure

CLIENT_ID=your_client_id_here
CLIENT_SECRET=your_client_secret_here
REFRESH_TOKEN=your_refresh_token_here
ACCESS_TOKEN=your_access_token_here

deleteRecords.js

require('dotenv').config();  // Load environment variables from .env file
const axios = require("axios");

// Load sensitive info from the .env file
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REFRESH_TOKEN = process.env.REFRESH_TOKEN;
const MODULE_NAME = "Leads"; // Change to Contacts, Deals, etc.

let ACCESS_TOKEN = ''; // Variable to hold the current access token

// Function to refresh the access token using the refresh token
async function refreshAccessToken() {
    try {
        const response = await axios.post('https://accounts.zoho.com/oauth/v2/token', null, {
            params: {
                refresh_token: REFRESH_TOKEN,
                client_id: CLIENT_ID,
                client_secret: CLIENT_SECRET,
                grant_type: 'refresh_token',
            },
        });

        // Store the new access token
        ACCESS_TOKEN = response.data.access_token;
        console.log('New Access Token:', ACCESS_TOKEN);

        return ACCESS_TOKEN;
    } catch (error) {
        console.error('Error refreshing access token:', error.response?.data || error.message);
        return null;
    }
}

// Function to fetch record IDs based on criteria with pagination
async function getRecordIds(criteria = 'Lead_Status:equals:Qualified') {
    let page = 1; // Start from page 1
    let allRecordIds = []; // Array to hold all record IDs
    let hasNextPage = true; // Flag to check if there are more pages

    while (hasNextPage) {
        try {
            const response = await axios.get(`${BASE_URL}/${MODULE_NAME}/search`, {
                headers: {
                    Authorization: `Zoho-oauthtoken ${ACCESS_TOKEN}`,
                },
                params: {
                    criteria: criteria,
                    page: page, // Current page
                },
            });

            // Log the response for debugging purposes
            console.log(`Fetched page ${page}:`, response.data);

            // If there are records, extract the record IDs and add to the array
            if (response.data && response.data.data && Array.isArray(response.data.data)) {
                const recordIds = response.data.data.map(record => record.id);
                allRecordIds = allRecordIds.concat(recordIds); // Add new IDs to the list
                console.log(`Fetched ${recordIds.length} records from page ${page}.`);
            }

            // Check if there are more records (next page)
            hasNextPage = response.data.info && response.data.info.next_page;
            page++; // Move to the next page if more records exist

        } catch (error) {
            if (error.response && error.response.status === 401) {
                // Access token is expired or invalid, refresh it and retry
                console.log("Access token expired. Refreshing the token...");
                const newAccessToken = await refreshAccessToken();
                if (newAccessToken) {
                    ACCESS_TOKEN = newAccessToken;
                    // Retry the request after refreshing the token
                    return getRecordIds(criteria); // Retry fetching records
                }
            }
            console.error("Error fetching records:", error.response?.data || error.message);
            hasNextPage = false; // Stop pagination on error
        }
    }

    console.log("All Record IDs fetched:", allRecordIds);
    return allRecordIds; // Return all fetched IDs
}

// Function to delete records in bulk
async function deleteRecords(recordIds) {
    if (recordIds.length === 0) {
        console.log("No records found to delete.");
        return;
    }

    try {
        const response = await axios.delete(`${BASE_URL}/${MODULE_NAME}`, {
            headers: {
                Authorization: `Zoho-oauthtoken ${ACCESS_TOKEN}`,
                "Content-Type": "application/json",
            },
            data: { ids: recordIds },
        });

        console.log(`Successfully deleted ${recordIds.length} records.`);
    } catch (error) {
        console.error("Error deleting records:", error.response?.data || error.message);
    }
}

// Execute - Get record IDs and delete them if needed
getRecordIds('Lead_Status:equals:Qualified') // You can change criteria here
    .then(recordIds => {
        if (recordIds.length > 0) {
            // Uncomment to delete records if needed
            // deleteRecords(recordIds);
        } else {
            console.log("No records found matching the criteria.");
        }
    });


    • Related Articles

    • Sticky Notes in Zoho CRM

      Sticky notes in Zoho CRM are a great way to quickly jot down details about a call, a task or any other information that needs to be easily accessible. These are personal notes and not visible to other users in your CRM account. Sticky notes are ...
    • Updating a Zoho CRM Subform (Accounts) from another Module (Deals)

      Updating a Zoho CRM Subform This script (courtesy of Zoho) will update existing records in a subform in one module (Accounts) from another Module (Deals). To create a cross-moduel subform entry (at creation for example), please see here ... ...
    • Event & Contact Sync between Office365 & Zoho CRM

      Connect your Meetings & Contacts between Zoho CRM and Microsoft Office 365.  in Office a new calender's is created called Zoho CRM, which you'll need to overlay in your Outlook. Events DO NOT sync to your personal calendar. The same applies to ...
    • Re-assigning your email address to the correct Zoho CRM instance

      Option 1: Deletes all Zoho services connected to your email address   1. Log into accounts.zoho.com (or accounts.zoho.eu depending on which server you're using) with your email address. 2. Click Preferences and select Close Account. This closes your ...
    • Zoho CRM for Outlook Add-In keeps disabling

      Why does this happen?  - Microsoft has some security measures in place to prevent slow add-ins from running inside Outlook. The issue is however that in many cases add-ins without fault are mistakenly marked as slow and disabled by Outlook, and if ...