# How to truncate a table from a rake file

**URL:** <https://www.ruby-forum.com/t/how-to-truncate-a-table-from-a-rake-file/163160>\
**Category:** Rails\
**Created:** [March 17, 2009, 8:36pm UTC](https://www.ruby-forum.com/t/how-to-truncate-a-table-from-a-rake-file/163160 "2009-03-17T20:36:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 17, 2009, 8:36pm UTC](https://www.ruby-forum.com/t/how-to-truncate-a-table-from-a-rake-file/163160/1 "2009-03-17T20:36:27Z")

</div>

I am new to rails and want to figure out the syntax for truncating a  
table using a rake file. Any ideas? thanks in advance!

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 17, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/how-to-truncate-a-table-from-a-rake-file/163160/2 "2009-03-17T20:51:35Z")

</div>

On Tue, Mar 17, 2009 at 12:36 PM, Chris G. \<  
removed\_email\_address@domain.invalid\> wrote:

> I am new to rails and want to figure out the syntax for truncating a  
> table using a rake file. Any ideas? thanks in advance!

Hi, what do you mean truncate a table? Do you mean delete the contents  
of  
the table or  
delete the table?

-Conrad

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 17, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/how-to-truncate-a-table-from-a-rake-file/163160/3 "2009-03-17T20:58:10Z")

</div>

Conrad T. wrote:

> On Tue, Mar 17, 2009 at 12:36 PM, Chris G. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > I am new to rails and want to figure out the syntax for truncating a  
> > table using a rake file. Any ideas? thanks in advance!
> 
> Hi, what do you mean truncate a table? Do you mean delete the contents  
> of  
> the table or  
> delete the table?
> 
> -Conrad

delete the contents of the table

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [March 17, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/how-to-truncate-a-table-from-a-rake-file/163160/4 "2009-03-17T23:09:10Z")

</div>

On Mar 17, 7:58 pm, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > delete the table?
> 
> > -Conrad
> 
> delete the contents of the table

If you make your task depend on the magic :environment task then  
activerecord will be loaded with your database configuration (and you  
models etc.) and you can just do MyModel.connection.execute “…” as  
you would have normally or just SomeModel.delete\_all if you don’t care  
about the difference between truncate and deleting all rows.

Fred
