Remove all pinned CIDs from your Infura account using the kubo-rpc-client and nodejs

Hello everyone,

The kubo-rpc-client JavaScript library can be used to remove all the objects pinned to your Infura IPFS account. Should you need to do it at some point, follow the next steps:

Install the library from the official kubo-rpc-client repository. Note that if you’ve previously used the ipfs-http-client to upload content to IPFS, it was deprecated and we recommend using the kubo client going forward.

Declare the IPFS client, list and export all your CIDs to an array and then remove all the elements of the array passing them through a for loop.
Don’t forget to use your API Key and API Key Secret.

:bulb: Also keep in mind that the kubo client can only be imported as a module, so for simplicity save the below piece of code as an mjs file, index.mjs for example, before running it in node.

Still if you wish to use .js files instead .mjs you will need to add the top-level field "type" with a value of "module" in the nearest package.json file as explained here.
You can also create an empty package.json file in your project directory and add the below to get it working as a .js file.

// package.json
{
  "type": "module"
}

So here’s the code:

import { create, CID } from "kubo-rpc-client";

const ApiKey= '2PP....XLU';
const ApiKeySecret= 'deb....89c';

const auth =
	  'Basic ' + Buffer.from(ApiKey + ':' + ApiKeySecret).toString('base64')

    const client = await create({
        host: 'ipfs.infura.io',
        port: 5001,
        protocol: 'https',
        headers: {
          authorization: auth
        }
      })
 


const cidValues=[];

async function listCidsthenUnpin() {  
    for await (const { cid, type } of client.pin.ls({type: 'recursive'})) {
            cidValues.push(cid.toString());
  }

 
  for ( let i=0 ; i <= cidValues.length -1; i++) {
   
for await (const cid of client.pin.rmAll(CID.parse(cidValues[i]))) {
 console.log(cid)
 }


}
}

listCidsthenUnpin();
2 Likes