Last active
          December 20, 2023 16:57 
        
      - 
      
- 
        Save stevedylandev/8f436ac7db9db8b36d9f54d2cf24dac6 to your computer and use it in GitHub Desktop. 
    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
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const PINATA_JWT = 'Bearer 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: 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: { | |
| accept: 'application/json', | |
| Authorization: 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() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
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();
`