Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:02:29+00:00 2026-06-09T12:02:29+00:00

I want to drop all tables in database db . All I do is

  • 0

I want to drop all tables in database db. All I do is iterate through db.tables and
drop them one by one. However, some tables are ignored and therefore not dropped. Then I
changed drop to truncate, every table is truncated. So, what’s wrong with drop?

Here are the codes:

controllers/admin.py:

def olddo_remove():
    response.view = 'admin/do_remove.html'
    l = []
    k = []
    for table_name in db.tables:
        l.append(table_name)
        db[table_name].drop()
        #db[table_name].truncate()
    return locals()

def do_remove():
    l = []
    k = []
    for table_name in db.tables:
        l.append(table_name)
    for table_name in l:
        k.append(table_name)
        db[table_name].drop()
    return locals()

views/admin/do_remove.html:

{{=l}}<br />{{=k}}

When I visit admin/olddo_remove, I get

['auth_user', 'auth_membership', 'auth_event', 'client', 'data']
[] 

When I visit admin/do_remove, I get

['auth_user', 'auth_group', 'auth_membership', 'auth_permission', 'auth_event', 'auth_cas', 'client', 'product', 'data']
['auth_user', 'auth_group', 'auth_membership', 'auth_permission', 'auth_event', 'auth_cas', 'client', 'product', 'data'] 

If I change the drop to truncate in olddo_remove, the output will be

['auth_user', 'auth_group', 'auth_membership', 'auth_permission', 'auth_event', 'auth_cas', 'client', 'product', 'data']
[] 

Why? What’s wrong?


This is going CRAZY!!

def test():
    response.view = 'admin/do_remove.html'
    l = db.tables
    k = []
    for table_name in l:
        k.append(table_name)
        db[table_name].drop()
    return locals()

Output:

['auth_group', 'auth_permission', 'auth_cas', 'product']
['auth_user', 'auth_membership', 'auth_event', 'client', 'data'] 

And only tables on the second line were dropped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:02:31+00:00Added an answer on June 9, 2026 at 12:02 pm

    This is a Python issue. db.tables is a list object (actually, it’s a special SQLCallableList object, which inherits from list). db[table_name].drop() not only drops the table_name table from the database but also removes table_name from the db.tables list. So, in the for loop, you are iterating over a list and mutating that list (i.e., deleting items from it) as you iterate. This is why you’re only getting every other table. In the example that works, you are first copying the list and then iterating over the copy (which is not getting mutated in the loop), so it works as expected. Truncation works even when iterating over db.tables directly because truncation doesn’t remove the tables (from the database or from db.tables).

    Note, as mentioned above, db.tables is a SQLCallableList. That means that db.tables() returns a copy of the db.tables list, so you can instead iterate over the copy:

    for table_name in db.tables():
        db[table_name].drop()
    

    The .drop() will remove the table_name from db.tables but will not remove it from the db.tables() copy over which the for loop is iterating, so everything should work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I drop selected tables in Rails? I want to drop all tables
I want to write a stored proc which iterates through all of the tables
If I want to delete all the tables in my database like this, will
I want to drop all tables and stored procedures in a schema, anyone know
I'm trying to delete all tables from a database except one, and I end
I don't want to drop database, because I'm not hosting the website on my
I want to drop all the schemas in a mysql db, and I don't
I want to drop some data from treeview to the datagridview temporarily, but the
I have a TcxDBColorCombobox and I want the drop down to not include the
There are many security reasons why one would want to drop an HTTP connection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.