Skip to content

Instantly share code, notes, and snippets.

@cachrisman
Created October 27, 2020 14:48
Show Gist options
  • Save cachrisman/aa639ef48fde3023b208fc04cdc17d8e to your computer and use it in GitHub Desktop.
Save cachrisman/aa639ef48fde3023b208fc04cdc17d8e to your computer and use it in GitHub Desktop.
A simple script to delete all the draft entries of the specified content type in Contentful
(async () => {
const contentful = require('contentful-management')
const parallel = require('async-parallel');
// change these to match your info
const space_id = 'your-space-id'
const environment_id = 'your-env-id'
const content_type = 'your-content-type-id'
const cmaToken = 'your-CMA-token'
const concurrency = 3 // adjust down if you are seeing many rate limit warnings
const client = contentful.createClient({accessToken: cmaToken})
const space = await client.getSpace(space_id)
const environment = await space.getEnvironment(environment_id)
parallel.setConcurrency(concurrency);
const getEntries = async () => {
let all_entries = []
let query = {
content_type: content_type,
'sys.publishedVersion[exists]': false, // this limits the query to draft entries
limit: 1
}
// this gets the total quantity of entries to be deleted
let entry_quantity = (await environment.getEntries(query)).total
// this gets all the entries of the specified query
for (let index = 0; index < entry_quantity / 1000; index++) {
query.limit = 1000
query.skip = index * 1000
let page = (await environment.getEntries(query)).items
all_entries = all_entries.concat(page)
}
return all_entries
}
const deleteEntry = async (entry) => {
try {
await entry.delete()
} catch (error) {
console.log(error)
}
}
const start = new Date()
const entries = await getEntries()
console.log(`Total entries: ${entries.length}`)
const result = await parallel.each(entries, deleteEntry)
const end = new Date()
const duration = (end - start)/1000
console.log(`Duration: ${duration} seconds`)
console.log(`Speed: ${(entries.length/duration).toFixed(2)} entries per second`)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment