1

Running pnp in modern script editor spfx web part, can add items, get items from a list, but cannot delete items from a list:

$pnp.sp.web.lists.getByTitle('List Name').items
    .filter('Title eq Test')
    .delete()

Error shows up in console:

DELETE https://xxx.sharepoint.com/sites/TeamIT/spfx-sfd/_api/web/lists/getByTitle('List Name')/items?$filter=Title eq Test 400 (Bad Request)

If I take the url and drop it into Chrome, it returns the item properly.

What on earth am I missing with something this simple? Lots of documentation in pnp on how to delete a list, didn't find much for list items, is there something unique that must be submitted with a delete to get it to operate properly?

  • You could refer my answer, hope it helps! – Sohail Shaikh Oct 24 '18 at 16:33
0

Try below code:

$pnp.sp.web.lists.getByTitle('List Name').items
    .filter('Title eq "Test"')
    .delete()
0

You delete it using batch request as below:

var batch = $pnp.sp.createBatch();

var list = $pnp.sp.web.lists.getByTitle("List Name");

list.items.filter("Title eq 'test'").get().then((items) =>{ 

  items.forEach(i =>{
    list.items.getById(i["ID"]).inBatch(batch).delete().then(r => {
      console.log("deleted");
    });
  });

  batch.execute().then(() => console.log("All deleted"));

});
  • This worked for me, looks like a syntax issue kept the above code from functioning: var batch = $pnp.sp.createBatch(); var varTitle = "Title eq '" + userObj + "'"; var list = $pnp.sp.web.lists.getByTitle("<List Name>"); list.items.filter(varTitle).get().then((items) =>{ items.forEach(i =>{ list.items.getById(i["ID"]).inBatch(batch).delete().then(r => { console.log("deleted"); }); }); batch.execute().then(() => console.log("Deleted item")); }); – Dodge1350 Oct 26 '18 at 16:17
  • @Dodge1350 - cool, happy to help. You can accept it as answer by ticking the checkbox besides the answer. – Gautam Sheth Oct 29 '18 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.