Delete Posts Trough CLI

Dears,

There’s a way to delete all posts using ghost-cli or API?

If not exist something like it, Can I delete all items of a specific table to delete all posts?

BR,

Are you trying to delete all posts or all content? If you’re trying to delete all your content, there’s an option to do that via the labs (/ghost/#/settings/labs).

If you’re looking to delete just posts, it might be easier to do that via the API.

// This code has not been tested. Use at your own risk!
const api = new GhostAdminAPI({
  url: 'http://localhost:2368',
  key: 'YOUR_ADMIN_API_KEY',
  version: "v3"
});

const {posts} = await api.posts.browse({limit: 'all'});

for (const post of posts) {
  console.log('Deleting %s - %s', post.id, post.title);
  await api.posts.delete({id: post.id})
}

Thanks for your reply, @vikaspotluri123!

I’m trying to delete all posts. I’m gonna test it!

I did create an entire IaC(Terraform + AWS) to provide and configure Ghost. I will create a serverless function to delete all posts for admin users. To do it, I will use lambda.

aws lambda invoke --function-name DeleteAllPosts --payload '{“admin_api_key”:“YOUR_ADMIN_API_KEY”} output.txt

Tried the below to delete all post and it is saying no method of api.post.delete?

const GhostContentAPI = require('@tryghost/content-api');
const api = new GhostContentAPI({
  url: 'https://demo.ghost.io',
  key: '22444f78447824223cefc48062',
  version: "v5.0"
});

// fetch 5 posts, including related tags and authors
api.posts
    .browse({limit: 5})
    .then((posts) => {
        posts.forEach((post) => {
            console.log(post.id);
            api.posts.delete({id: post.id})
        });
    })
    .catch((err) => {
        console.error(err);
    });

The Content API won’t let you delete posts, because it’s a read-only API. If you’re trying to mutate data, you need to use the Admin API with an Admin API Key