Skip to content

Instantly share code, notes, and snippets.

@stevedylandev
Last active June 11, 2023 03:19
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
This script will unpin all files from your Pinata account! You can alter the PIN_QUERY to adjust it if you only want to delete a range of files
const axios = require('axios')
const PINATA_JWT = 'Bearer PASTE_YOUR_JWT'
const PIN_QUERY = 'https://api.pinata.cloud/data/pinList?status=pinned&includesCount=false&pageLimit=1000'
let pinHashes = []
const deletePinFromIPFS = async (hashToUnpin) => {
try {
const res = await axios.delete(`https://api.pinata.cloud/pinning/unpin/${hashToUnpin}`, {
headers: {
Authorization: PINATA_JWT
}
})
console.log(res.status)
} catch (error) {
console.log(error)
}
}
const wait = async (time) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time)
});
}
const fetchPins = async () => {
try {
const res = await axios.get(PIN_QUERY, {
headers: {
Authorization: PINATA_JWT
}
})
const responseData = res.data.rows
responseData.forEach(row => {
pinHashes.push(row.ipfs_pin_hash)
})
console.log(pinHashes)
} catch (error) {
console.log(error)
}
}
const bulkUnpin = async () => {
try {
for(const hash of pinHashes){
await deletePinFromIPFS(hash)
await wait(200)
}
pinHashes = []
} catch (error){
console.log(error)
}
}
const main = async () => {
await fetchPins()
while(pinHashes){
await bulkUnpin()
await fetchPins()
}
}
main()
@JonathanConn
Copy link

JonathanConn commented Feb 23, 2023

Modified version for pinata submarine

`const axios = require('axios');
const PIN_QUERY =
'https://managed.mypinata.cloud/api/v1/content?status=pinned&includesCount=false&pageLimit=1000';

let pinHashes = [];

const PINATA_SUB_KEY = ''; // add your sub api key here

const deletePinFromIPFS = async (hashToUnpin) => {
try {
const res = await axios.delete(https://managed.mypinata.cloud/api/v1/content/${hashToUnpin}, {
headers: {
'x-api-key': PINATA_SUB_KEY,
},
});
console.log('successfully unpinned: ', hashToUnpin);
} catch (error) {
console.log('failed to unpin: ', hashToUnpin);
}
};
const wait = async (time) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
});
};

const fetchPins = async () => {
try {
const res = await axios.get(PIN_QUERY, {
headers: {
'x-api-key': PINATA_SUB_KEY,
},
});
// console.log(res.data.items);
const responseData = res.data.items;
responseData.forEach((row) => {
pinHashes.push(row.cid);
});
// console.log(pinHashes);
} catch (error) {
console.log('failed to fetch pin');
}
};

const bulkUnpin = async () => {
try {
for (const hash of pinHashes) {
await deletePinFromIPFS(hash);
await wait(200);
}
pinHashes = [];
} catch (error) {
console.log('failed to bulkunpin');
}
};

const main = async () => {
await fetchPins();
while (pinHashes) {
await bulkUnpin();
await fetchPins();
}
};

main();
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment