The process of removing files from IPFS is called unpinning. When you unpin something from an IPFS storage node, it is marked for garbage collection. When garbage collection runs, the content is permanently deleted from the storage node.
Keep in mind that if someone else has pinned your content on IPFS, it will
remain available even if you delete your files!
Deleting Programatically
The SDK has a very simple unpin method that will allow you to delete an array of cids
.
import { PinataSDK } from "pinata-web3";
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT!,
pinataGateway: "example-gateway.mypinata.cloud",
});
const unpin = await pinata.unpin([
"bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7garjiubll2ceym4",
"QmVLwvmGehsrNEvhcCnnsw5RQNseohgEkFNN1848zNzdng"
])
Deleting All Files
If you find yourself in a place where you need to unpin a lot of files or perhaps all your files, you can use a script like this to create an array of CIDs and unpin them one by one. The example below uses the pinList
queries to target all pinned files and return 1000 for each request. This could easily be done with a different query to target different files, please check out the listing files doc for more info.
const PINATA_JWT = "YOUR_JWT_HERE";
const PIN_QUERY = `https://api.pinata.cloud/data/pinList?status=pinned&pageLimit=1000&includeCount=false`;
const fetch = require("node-fetch");
const wait = (milliseconds) => {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
};
const fetchPins = async () => {
try {
console.log("Fetching pins...");
let pinHashes = [];
let pageOffset = 0;
let hasMore = true;
while (hasMore === true) {
try {
const response = await fetch(`${PIN_QUERY}&pageOffset=${pageOffset}`, {
method: "GET",
headers: {
accept: "application/json",
Authorization: `Bearer ${PINATA_JWT}`,
},
});
const responseData = await response.json();
const rows = responseData.rows;
if (rows.length === 0) {
hasMore = false;
}
const itemsReturned = rows.length;
pinHashes.push(...rows.map((row) => row.ipfs_pin_hash));
pageOffset += itemsReturned;
await wait(300);
} catch (error) {
console.log(error);
break;
}
}
console.log("Total pins fetched: ", pinHashes.length);
return pinHashes;
} catch (error) {
console.log(error);
}
};
const deletePins = async () => {
const pinHashes = await fetchPins();
const totalPins = pinHashes.length;
let deletedPins = 0;
try {
for (const hash of pinHashes) {
try {
const response = await fetch(
`https://api.pinata.cloud/pinning/unpin/${hash}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${PINATA_JWT}`,
},
}
);
await wait(300);
deletedPins++;
process.stdout.write(`Deleted ${deletedPins} of ${totalPins} pins\r`);
} catch (error) {
console.log(error);
}
}
console.log("Pins deleted");
} catch (error) {
console.log(error);
}
};
deletePins();
Deleting by Web App
If you are trying to delete files (or in IPFS terms “Unpin”), you can do so by clicking on the “more” button and selecting “Unpin File”
Additionally, with our Bulk File Actions tool, you can select and manage multiple files at once - up to 100!