You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用watson-developer-cloud Node.js SDK批量删除多个Intent?

How to Delete Multiple Intents with Watson Developer Cloud Node.js SDK

The deleteIntent method in the Watson Node.js SDK only handles deleting a single intent at a time—since the intent parameter expects a single string name. To delete multiple intents, you'll need to run multiple delete operations, either sequentially or in parallel. Here's how to do it cleanly:

Step 1: Ensure you have an array of intent names

First, make sure the IntentName value from your request is an array (if your frontend sends a single intent, wrap it in an array to keep the code consistent):

const IntentName = req.body.intentName;
// Normalize to array format
const intentNames = Array.isArray(IntentName) ? IntentName : [IntentName];

Step 2: Run parallel delete operations with Promise.all

The SDK's methods support Promises (you can avoid the callback syntax for cleaner async code). We'll create an array of delete Promises and run them all at once:

// Create a Promise for each intent deletion
const deleteOperations = intentNames.map(intentName => {
  const params = {
    workspace_id: workspaceId,
    intent: intentName
  };
  // Return the Promise version of deleteIntent
  return conversation.deleteIntent(params);
});

// Execute all deletions in parallel
Promise.all(deleteOperations)
  .then(successResponses => {
    console.log('All intents deleted successfully:', JSON.stringify(successResponses, null, 2));
  })
  .catch(error => {
    console.error('Error deleting one or more intents:', error);
  });

Step 3: Alternative: Use async/await for readability

If you're working inside an async function, you can use await with Promise.all for more readable code:

async function bulkDeleteIntents() {
  const IntentName = req.body.intentName;
  const intentNames = Array.isArray(IntentName) ? IntentName : [IntentName];

  try {
    const results = await Promise.all(
      intentNames.map(intentName => {
        const params = {
          workspace_id: workspaceId,
          intent: intentName
        };
        return conversation.deleteIntent(params);
      })
    );
    console.log('Bulk deletion complete:', JSON.stringify(results, null, 2));
  } catch (err) {
    console.error('Failed during bulk deletion:', err);
  }
}

// Call the async function
bulkDeleteIntents();

Important Notes

  • Handle partial failures: If you want to continue deleting other intents even if one fails, use Promise.allSettled instead of Promise.all. This will return results for every operation (success or failure), which you can filter later.
  • Legacy SDK versions: If your SDK version doesn't support Promises natively, use Node.js's util.promisify to convert the callback-based method:
    const util = require('util');
    const deleteIntentAsync = util.promisify(conversation.deleteIntent.bind(conversation));
    
    // Then use deleteIntentAsync(params) in your map function
    

内容的提问来源于stack exchange,提问作者Yahya Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:42:35