def trim_tables():
 ignore_fields = (default_fields + optional_fields)
 for doctype in frappe.db.get_all(u'DocType', filters={u'issingle': 0}):
  doctype = doctype.name
  columns = frappe.db.get_table_columns(doctype)
  fields = frappe.get_meta(doctype).get_fieldnames_with_value()
  columns_to_remove = [f for f in list((set(columns) - set(fields))) if ((f not in ignore_fields) and (not f.startswith(u'_')))]
  if columns_to_remove:
   print doctype, u'columns removed:', columns_to_remove
   columns_to_remove = u', '.join([u'drop `{0}`'.format(c) for c in columns_to_remove])
   query = u'alter table `tab{doctype}` {columns}'.format(doctype=doctype, columns=columns_to_remove)
   frappe.db.sql_ddl(query)

def trim_tables ( ) : ignore_fields = ( default_fields + optional_fields ) for doctype in frappe . db . get_all ( u'DocType' , filters = { u'issingle' : 0 } ) : doctype = doctype . name columns = frappe . db . get_table_columns ( doctype ) fields = frappe . get_meta ( doctype ) . get_fieldnames_with_value ( ) columns_to_remove = [ f for f in list ( ( set ( columns ) - set ( fields ) ) ) if ( ( f not in ignore_fields ) and ( not f . startswith ( u'_' ) ) ) ] if columns_to_remove : print doctype , u'columns removed:' , columns_to_remove columns_to_remove = u', ' . join ( [ u'drop `{0}`' . format ( c ) for c in columns_to_remove ] ) query = u'alter table `tab{doctype}` {columns}' . format ( doctype = doctype , columns = columns_to_remove ) frappe . db . sql_ddl ( query )
use this to remove columns that dont exist in meta .