Managing Filters

You can use Filters to configure advanced filtering rules for an account. Filters can automatically add or remove labels or forward emails to verified aliases based on the attributes or content of the incoming message.

For information on how to create, list, get, or delete filters, see the Filters reference.

Matching criteria

You can filter messages by properties such as the sender, subject date, size, and message contents. Any query using Gmail's advanced search syntax can also be used in a filter. For example, common filter patterns include:

Filter Matches
criteria.from='' All emails from
All emails larger than 10MB
criteria.hasAttachment=true All emails with an attachment
criteria.subject='[People with Pets]' All emails with the string [People with Pets] in the subject
criteria.query='"my important project"' All emails containing the string my important project
critiera.negatedQuery='"secret knock"' All emails that do not contain the string secret knock

If multiple criteria are present in a filter, a message must satisfy all crtieria for the filter to apply.


You can apply an action to messages matching the filter criteria. Messages may be forwarded to a verified email address, or have labels added or removed.

You can add or remove labels to change the disposition of the email. For example, some common actions include:

Action Effect
action.removeLabelIds=['INBOX'] Archive the email
action.removeLabelIds=['UNREAD'] Mark as read
action.addLabelIds=['TRASH'] Delete the email
action.addLabelIds=['STARRED'] Mark as starred
action.addLabelIds=['<user label id>'] Tag the mail with a user defined label


The following is a more complete example showing how to label and archive messages from a mailing list.


String labelId = "Label_14"; // ID of the user label to add
Filter filter = new Filter()
        .setCriteria(new FilterCriteria()
        .setAction(new FilterAction()
Filter result = gmailService.users().settings().filters().create("me", filter).execute();
System.out.println("Created filter " + result.getId());


label_id = 'Label_14' # ID of user label to add
filter = {
    'criteria': {
        'from': ''
    'action': {
        'addLabelIds': [label_id],
        'removeLabelIds': ['INBOX']
result = gmail_service.users().settings().filters().\
    create(userId='me', body=filter).execute()
print 'Created filter: %s' % result.get('id')

Send feedback about...