title: Deleting Specific Emails in Gmail using Python
body: <p>I keep receiving this error when I run the script that is suppose to delete emails from a specific sender ONLY:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/Users/philip/Desktop/Python Projects/email_organizer/eorger.py&quot;, line 33, in &lt;module&gt;
      deleteEmail(email,pwd,imapserver)
    File &quot;/Users/philip/Desktop/Python Projects/email_organizer/eorger.py&quot;, line 24, in deleteEmail
      typ,data=mail.search(None,'Robinhood@prospectusdocs.com')
    File &quot;/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py&quot;, line 734, in search
      typ, dat = self._simple_command(name, *criteria)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py&quot;, line 1230, in _simple_command
      return self._command_complete(name, self._command(name, *args))
    File &quot;/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py&quot;, line 1055, in _command_complete
      raise self.error('%s command error: %s %s' % (name, typ, data))
  imaplib.error: SEARCH command error: BAD [b'Could not parse command']
  </code></pre>
  <p>Here is my code that produces this error:</p>
  <pre><code>import imaplib
  
  # your email address
  email='test@gmail.com'
  # your email password
  pwd='************'
  
  # need to input imap server to connect to gmail
  imapserver='imap.gmail.com'
  
  def deleteEmail(email,pwd,IMAP):
      mail=imaplib.IMAP4_SSL(IMAP) # connect to server
      mail.login(email,pwd) # user server to login to gmail account
  
      mail.select('inbox') # selects the 'inbox' to look into
      # search for specific criteria in selected inbox
      typ,data=mail.search(None,'Robinhood@prospectusdocs.com')
  
      for num in data[0].split():
          mail.store(num,'+FLAGS',r'(\Deleted)')
  
      mail.expunge()
      mail.close()
      mail.logout()
  
  deleteEmail(email,pwd,imapserver)
  </code></pre>
  <p>Any advice?</p>