jackwener/OpenCLI · error

Failed to delete collection: HTTP ${res.status}${body ? ' -

Error message

Failed to delete collection: HTTP ${res.status}${body ? ' - ' + body.slice(0, 200) : ''}

What it means

Thrown when the final POST that deletes the resolved collection returns non-2xx. The status code and first 200 chars of Instagram's response body are embedded to expose the reason (auth, rate limit, already deleted). It is the transport-level failure guard for the delete call.

Source

Thrown at clis/instagram/collection-delete.js:78

    if (matches.length > 1) {
      const ids = matches.map((c) => c.collection_id).join(', ');
      throw new Error('Multiple collections share the name "' + raw + '" (ids: ' + ids + '). Pass the numeric collection_id explicitly to disambiguate.');
    }
    id = String(matches[0].collection_id);
    resolvedName = String(matches[0].collection_name || raw);
  }

  const fd = new FormData();
  fd.append('module_name', 'collection_settings');
  const res = await fetch('https://www.instagram.com/api/v1/collections/' + encodeURIComponent(id) + '/delete/', {
    method: 'POST',
    credentials: 'include',
    headers: { ...headers, 'X-CSRFToken': csrf },
    body: fd,
  });
  if (!res.ok) {
    const body = await res.text().catch(() => '');
    throw new Error('Failed to delete collection: HTTP ' + res.status + (body ? ' - ' + body.slice(0, 200) : ''));
  }
  const d = await res.json().catch(() => ({}));
  if (d?.status && d.status !== 'ok') {
    throw new Error('Instagram returned non-ok status: ' + JSON.stringify(d).slice(0, 300));
  }
  return [{
    status: 'Deleted',
    collectionId: id,
    collectionName: resolvedName,
  }];
})()
` },
    ],
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Read the body slice for Instagram's specific error message
  2. Re-authenticate to refresh csrftoken/session if 401/403
  3. Add backoff and retry for 429 rate-limit responses
  4. Re-resolve the collection (it may no longer exist) before retrying

Example fix

// before
throw new Error('Failed to delete collection: HTTP ' + res.status + ...);
// after
if (res.status === 429) { await sleep(60000); return deleteCollection(target); }
throw new Error('Failed to delete collection: HTTP ' + res.status + ...);
Defensive patterns

Strategy: retry

Validate before calling

// Pre-resolve and confirm the collection still exists before deleting
const list = await listCollections();
if (!list.some((c) => String(c.collection_id) === id)) throw new Error('Collection already gone: ' + id);

Type guard

function isRetryableHttpStatus(msg: string): boolean {
  const m = msg.match(/HTTP (\d+)/);
  return !!m && ['408', '429', '500', '502', '503', '504'].includes(m[1]);
}

Try / catch

try {
  await deleteCollection(id);
} catch (e) {
  const m = String(e.message).match(/HTTP (\d+)/);
  if (m && m[1] === '429') { await sleep(60000); return deleteCollection(id); }
  if (m && m[1] === '403') { await relogin(); return deleteCollection(id); }
  throw e;
}

Prevention

When it happens

Trigger: Deleting with an expired CSRF token/session (403), rate limiting (429), the collection having been deleted concurrently (404), or Instagram API changes to the delete endpoint.

Common situations: Long-running scripts whose session expires mid-deletion; batch delete loops hitting rate limits; races where another client deleted the collection first.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/7aa0ab59525fbdcd. Report an issue: GitHub.