# Delete All action

**URL:** <https://www.ruby-forum.com/t/delete-all-action/156043>\
**Category:** Rails\
**Created:** [December 19, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/delete-all-action/156043 "2008-12-19T18:21:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 19, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/delete-all-action/156043/1 "2008-12-19T18:21:22Z")

</div>

Hi,

What is the best way to delete all members of a model?

For example I have a tasks model and want to put a link in that will  
delete all tasks at once. Something like,

@tasks = Task.all  
@tasks.each { |task| task.destroy }

Do I need another action for this, and what should the link to it be?

cheers,

DAZ

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 19, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/delete-all-action/156043/2 "2008-12-19T18:34:51Z")

</div>

On Fri, Dec 19, 2008 at 12:20 PM, DAZ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> What is the best way to delete all members of a model?
> 
> For example I have a tasks model and want to put a link in that will  
> delete all tasks at once. Something like,
> 
> @tasks = Task.all  
> @tasks.each { |task| task.destroy }

If you need to allow callbacks to run or dependent associations to also  
be  
destroyed, you can use Task.destroy\_all. If you don’t need those things,  
Task.delete\_all will be much faster.

> Do I need another action for this, and what should the link to it be?

You could add another route. For example,

map.resources :tasks, :collection =\> { :destroy\_all =\> :delete }

In TasksController, you’d add something like

def destroy\_all  
Task.destroy\_all

# redirect, render some RJS, etc.

end

In a view, you could

link\_to “Delete all”, destroy\_all\_tasks\_path

You might want to put a confirmation on the link\_to.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 21, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/delete-all-action/156043/3 "2008-12-21T20:46:30Z")

</div>

cheers Craig, those instructions were really clear and worked  
perfectly.

Thanks again,

DAZ

On Dec 19, 5:33 pm, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 30, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/delete-all-action/156043/4 "2008-12-30T12:19:46Z")

</div>

I’d now like to take this a step further and allow users to select  
which tasks they want to delete (using check boxes probably) then  
select ‘delete all checked tasks’. Does anybody have any pointers on  
how to go about this and how the form would look? I’m guessing that I  
would need to create a @selected\_tasks array somehow from what was  
submitted and then call delete\_all on that?

cheers,

DAZ
