def clear_cache(modeladmin, request, queryset=None):
    """
    Clears media cache files such as thumbnails.
    """
    
    execute = request.POST.get('execute')
    
    files_in_storage = []
    storage = get_media_storage()
    cache_files_choices = []
    for storage_name in get_cache_files():
        link = mark_safe('<a href="%s">%s</a>' % (
            storage.url(storage_name), storage_name))
        cache_files_choices.append((storage_name, link))

    if not len(cache_files_choices):
        messages.warning(request, message=_('There are no cache files.'))
        return HttpResponseRedirect('')

    if execute:
        form = DeleteCacheFilesForm(queryset, cache_files_choices, request.POST)
        if form.is_valid():
            form.save()
            node = FileNode.get_top_node()
            message = ungettext('Deleted %i cache file.', 'Deleted %i cache files.', len(form.success_files)) % len(form.success_files)
            if len(form.success_files) == len(cache_files_choices):
                message = '%s %s' % (_('The cache was cleared.'), message)
            messages.success(request, message=message)
            if form.error_files:
                messages.error(request, message=_('The following files could not be deleted:')+' '+repr(form.error_files))
            return HttpResponseRedirect(node.get_admin_url())

    if not execute:
        if len(cache_files_choices) > 0:
            form = DeleteCacheFilesForm(queryset, cache_files_choices)
        else:
            form = None

    c = get_actions_context(modeladmin)
    c.update({
        'title': _('Clear cache'),
        'submit_label': _('Delete selected files'),
        'form': form,
        'select_all': 'selected_files',
    })
    return render_to_response('admin/media_tree/filenode/actions_form.html', c, context_instance=RequestContext(request))

    return HttpResponseRedirect('')
Clears media cache files such as thumbnails.