Jump to content
Valery

Empty Trash via the API

Recommended Posts

Hello everybody,

I am trying to empty the Trash through the API. What's the best way to accomplish that?

Also, does

$pages->get("parent=/trash/"); 

work?

Share this post


Link to post
Share on other sites

That will get the pages. one page [and you need include all {of course}, see below].you then need to delete them :D...

Edited for corrections..see below...slow Tuesday kongondo!

Edited by kongondo

Share this post


Link to post
Share on other sites

To delete pages you may use delete. http://cheatsheet.processwire.com/?filter=delete (must have been still in m clipboard :P )

$pages->delete($page, true); // recursive with children

$pages->delete($page);

$page->delete();

To get pages from the trash you would need include all

$pa = $pages->find("parent=/trash/,include=all");

$pages->get() will give you only 1 page. http://cheatsheet.processwire.com/?filter=pages-%3Eget

  • Like 5

Share this post


Link to post
Share on other sites

I did not try the following code, but I think it should work:

foreach ($pages->find("status>=" . Page::statusTrash) as $p) {

$p->delete();

}

Edit: Soma was faster again..
  • Like 3

Share this post


Link to post
Share on other sites
On 8/20/2013 at 7:46 AM, Valery said:

I am trying to empty the Trash through the API. What's the best way to accomplish that?


I know this is ancient, but I just had to do this and discovered a very simple way :)

$pages->emptyTrash();

Maybe this is newish?

  • Like 3

Share this post


Link to post
Share on other sites
8 hours ago, adrian said:


I know this is ancient, but I just had to do this and discovered a very simple way :)


$pages->emptyTrash();

Maybe this is newish?

Nice one! Seems it's been around since 2.7...so, yeah, newish

  • Like 3

Share this post


Link to post
Share on other sites

I'm trying to empty the trash with a hook but it's not working:

$wire->addHook('LazyCron::everyMinute', function() {

  $trashed_pages = wire('pages')->find("parent=/trash/, include=all");

  if(count($trashed_pages)) {
    $trashed_pages->emptyTrash();
  }

});

 

Share this post


Link to post
Share on other sites

I figured it out:

$wire->addHook('LazyCron::every4Weeks', function() {

  $trashed_pages = wire('pages')->find("parent=/trash/, include=all");

  if(count($trashed_pages)) {
    wire('pages')->emptyTrash();
  }

});

Should I use the above or just the below simpler version?

$wire->addHook('LazyCron::every4Weeks', function() {

  wire('pages')->emptyTrash();

});

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Piguanet
      Hola, soy bastante nueva en esto. Tengo en la papelera de mi CMS 4300 aprox. archivos que no puedo borrar. Cuando le doy la opción vaciar me dice que no se puede y continuan ahi.

       
    • By celfred
      Hello,
      I have a feeling I'm missing an easy thing here (again...). If I trash a page from API with :
      mypage->trash()
      Is there a simple way to restore it if needed ? I can do it in back-end, but I'd like to do it through API with something like
      mypage->restore()
       but this doesn't exit  
      So if you can give me a little help on that, I'd appreciate. Thanks in advance !
    • By gerald
      Hi all,
      I use version 3.0.62. Is there a way to delete all children of a page with one click - without parent page?
      thanks
    • By Robin S
      PageListTrash
      Allows non-superusers to trash pages directly from Page List (if they have page-delete permission for that page).
      Not much to say really - the module adds a "Trash" option to the extra actions for pages in Page List. It looks and works just like the Trash action available to superusers.

      https://github.com/Toutouwai/PageListTrash/
       
      Up to you whether you think non-superusers should be trusted with simpler trashing. For most cases I like the default behaviour where editors have to jump through some more hoops - I want them to think carefully about what they are doing. But if an editor needs to trash several pages then this module might reduce frustration.
      @tpr, by now you can probably predict what I'm going to say...
      ...something to merge into AdminOnSteroids?
    • By alexcapes
      Hi,
      I've noticed when I trash pages from a PageTable field, then restore them, they are restored to their original location, however the connection is broken with the PageTable field.
      Is this expected behaviour? Is there a way for it to maintain this relationship? Otherwise restoring it doesn't actually restore it to it's previous state.
      Note that in this case the parent pages for the PageTable field is not set as the direct parent.
       
       
       
×
×
  • Create New...