def clean_old_index(model, block_size=<NUM_LIT:100>, **kwargs):
conn = _connect(model)<EOL>version = list(map(int, conn.info()['<STR_LIT>'].split('<STR_LIT:.>')[:<NUM_LIT:2>]))<EOL>has_hscan = version >= [<NUM_LIT:2>, <NUM_LIT:8>]<EOL>pipe = conn.pipeline(True)<EOL>prefix = '<STR_LIT>'%model._namespace<EOL>index = prefix + '<STR_LIT::>'<EOL>block_size = max(block_size, <NUM_LIT:10>)<EOL>force_hscan = kwargs.get('<STR_LIT>', False)<EOL>if (has_hscan or force_hscan) and force_hscan is not None:<EOL><INDENT>max_id = conn.hlen(index)<EOL>cursor = None<EOL>scanned = <NUM_LIT:0><EOL>while cursor != b'<STR_LIT:0>':<EOL><INDENT>cursor, remove = _scan_index_lua(conn, [index, prefix], [cursor or '<STR_LIT:0>', block_size, <NUM_LIT:0>, <NUM_LIT:0>])<EOL>if remove:<EOL><INDENT>_clean_index_lua(conn, [model._namespace], remove)<EOL><DEDENT>scanned += block_size<EOL>if scanned > max_id:<EOL><INDENT>max_id = scanned + <NUM_LIT:1><EOL><DEDENT>yield scanned, max_id<EOL><DEDENT>for uniq in chain(model._unique, model._cunique):<EOL><INDENT>name = uniq if isinstance(uniq, six.string_types) else '<STR_LIT::>'.join(uniq)<EOL>idx = prefix + name + '<STR_LIT>'<EOL>cursor = None<EOL>while cursor != b'<STR_LIT:0>':<EOL><INDENT>cursor, remove = _scan_index_lua(conn, [idx, prefix], [cursor or '<STR_LIT:0>', block_size, <NUM_LIT:1>, <NUM_LIT:0>])<EOL>if remove:<EOL><INDENT>conn.hdel(idx, *remove)<EOL><DEDENT>scanned += block_size<EOL>if scanned > max_id:<EOL><INDENT>max_id = scanned + <NUM_LIT:1><EOL><DEDENT>yield scanned, max_id<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if model._unique or model._cunique:<EOL><INDENT>if has_hscan:<EOL><INDENT>warnings.warn("<STR_LIT>", stacklevel=<NUM_LIT:2>)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn("<STR_LIT>", stacklevel=<NUM_LIT:2>)<EOL><DEDENT><DEDENT>max_id = int(conn.get('<STR_LIT>'%(prefix, model._pkey)) or '<STR_LIT:0>')<EOL>for i in range(<NUM_LIT:1>, max_id+<NUM_LIT:1>, block_size):<EOL><INDENT>ids = list(range(i, min(i+block_size, max_id+<NUM_LIT:1>)))<EOL>for id in ids:<EOL><INDENT>pipe.exists(prefix + str(id))<EOL>pipe.hexists(index, id)<EOL><DEDENT>result = iter(pipe.execute())<EOL>remove = [id for id, ent, ind in zip(ids, result, result) if ind and not ent]<EOL>if remove:<EOL><INDENT>_clean_index_lua(conn, [model._namespace], remove)<EOL><DEDENT>yield min(i+block_size, max_id-<NUM_LIT:1>), max_id<EOL><DEDENT><DEDENT>yield max_id, max_id<EOL>
This utility function will clean out old index data that was accidentally
left during item deletion in rom versions <= 0.27.0 . You should run this
after you have upgraded all of your clients to version 0.28.0 or later.

Arguments:

    * *model* - the model whose entities you want to reindex
    * *block_size* - the maximum number of items to check at a time
      defaulting to 100

This function will yield its progression through re-checking all of the
data that could be left over.

Example use::

    for progress, total in clean_old_index(MyModel, block_size=200):
        print "%s of %s"%(progress, total)