Delete all expired tasks. Just decide yourself what is meant with
expired. (finished, failed, ...)

@param DateTime $newest (optional)

@return int
545077
public function purge(DateTime $newest = null)
    {

        $deleteIds = [];

        /**
         * @var int $taskId
         * @var Task $task
         */
        foreach ($this->tasks as $taskId=>$task) {
            if ($task->getState() == Queue::FINISHED) {
                $deleteIds[] = $taskId;
            }
        }

        foreach ($deleteIds as $taskId) {
            unset($this->tasks[$taskId]);
        }

        return count($deleteIds);
    }
684951
public function getLastAccessed(): \DateTime
    {
        \clearstatcache();
        if (false === $atime = \fileatime($this->filename)) {
            throw new TimeCanNotBeReadException('Last access time can not be read.');
        }
        return (new \DateTime())->setTimestamp($atime);
    }
136156
public function expired($limit = 10) 
    {
		$main = $this->getServiceLocator()->get('neobazaar.service.main');
		$em = $main->getEntityManager();
		$cache = $this->getServiceLocator()->get('ClassifiedCache');
		$documentRepository = $main->getDocumentEntityRepository();
		$documents = $documentRepository->getExpiredElegibleToMailSend($limit); 
        $url = $this->getServiceLocator()->get('ControllerPluginManager')->get('Url');

		$ids = array();
		foreach ($documents as $document) {
    	    $classifiedModel = $this->getServiceLocator()->get('document.model.classifiedAdminListing');
    	    $classifiedModel->init($document, $this->getServiceLocator());
		    $event = new ClassifiedExpiredEvent(__FUNCTION__, null, array(
    		    'to' => $classifiedModel->email, 
    		    'siteurl' => $url->fromRoute('home', array(), array('force_canonical' => true)),
    		    'fullname' => $classifiedModel->fullname,
		        'title' => $classifiedModel->title, 
		        'hash' => $classifiedModel->hashId, 
		        'content' => $classifiedModel->content,
		        'hits' => $classifiedModel->visited
    		));
		    
		    $this->getEventManager()->trigger(ClassifiedExpiredEvent::EVENT_CLASSIFIED_EXPIRED_PRE, $this, $event);
			
			$document->setState(Document::DOCUMENT_STATE_DEACTIVE);
			$document->setVisited($document->getVisited() + 1); // this is a trick to make it not update edit date
			$em->persist($document);
			$em->flush();
			$ids[] = $document->getDocumentId();		
			$key = $documentRepository->getEncryptedId($document->getDocumentId());
			if ($cache->hasItem($key)) {
				$cache->removeItem($key);
			}
			
		    $this->getEventManager()->trigger(ClassifiedExpiredEvent::EVENT_CLASSIFIED_EXPIRED_POST, $this, $event);
		}
		
		return $ids;
    }
875819
public function isExpired()
    {
        if (is_object($this->ExpireAtDate)) {
            $this->ExpireAtDate = $this->ExpireAtDate->Value;
        }

        $expired = false;

        if (!empty($this->ExpireAtDate)) {
            if (SS_Datetime::now()->Value >= $this->ExpireAtDate) {
                $expired = true;
            }
        }

        return $expired;
    }
882365
public function isExpired()
    {
        if (isset($this->_claims['exp']) && is_numeric($this->_claims['exp']))
        {
            $now  = new DateTime('now');
            return ($now->format('U') - $this->_claims['exp']) > 0;
        }

        return false;
    }
566512
public function isExpired(DateTime $date = null)
    {
        // Here we'll determine if the account expires by checking is expiration date.
        if ($expirationDate = $this->expirationDate()) {
            $date = $date ?: new DateTime();

            return $expirationDate <= $date;
        }

        // The account has no expiry date.
        return false;
    }
960160
public function isExpired()
    {
        if (null === $this->expiresAt) {
            return false;
        }

        $now = new \DateTime();

        return $this->expiresAt < $now;
    }
142615
public function pruneAttempts(DateTime $before = null)
    {
        if (!$before) {
            $duration = (int) $this->config->get('concrete.user.deactivation.authentication_failure.duration');
            $before = Carbon::now('UTC')->subSeconds($duration);
        }

        // Load our repository and get entries before our date
        $repository = $this->entityManager->getRepository(LoginAttempt::class);
        $results = $repository->before($before);

        // Loop through and remove those entries
        $batch = [];
        $max = 50;

        foreach ($results as $result) {
            $this->entityManager->remove($result);
            $batch[] = $result;

            // Handle the batch
            $batch = $this->manageBatch($batch, $max);
        }

        // Close off the batch
        $this->manageBatch($batch);
    }
388604
public function flushExpired()
    {
        $this->Logger->debug('Flush all expired called.');

        try {
            foreach ( new DirectoryIterator($this->dir) as $item ) {
                $filename = $item->getPath() .DIRECTORY_SEPARATOR.$item->getFilename();
                $pathinfo = pathinfo($filename);

                // Find all files ending with .cfcache and destroy them if expired!
                if ($pathinfo['extension'] == $this->fileExtension)
                    $data = $this->readFile($filename);
                    if ($this->isExpired(null, $data))
                        unlink($filename);
            }
        } catch (Exception $e) {
            $this->Logger->error("flushExpired error: " . $e->getMessage());
            return false;
        }

        return true;
    }
178421
private function isExpired($createTs, $lifetime) {
    if ($lifetime === null) {
      return false;
    }
    $expireDate = (new \DateTime())->setTimeStamp($createTs);
    if (intval($lifetime)) {
      $expireDate = $expireDate->modify('+'.$lifetime.' seconds');
    }
    return $expireDate < new \DateTime();
  }
204723
public function purgeExpired()
    {

        if ($this->sqlInitialized()) {
            if ($this->sql) {
                if ($this->sql->query("
                    DELETE
                        FROM `{$this->table}`
                        WHERE `expire` < NOW()
                ")) {
                    return true;
                }
            }
        }
        return false;
    }
200100
public function cleanUp( $expires = 0 )
	{
		$expires	= $expires ? $expires : $this->expires;
		if( !$expires )
			throw new InvalidArgumentException( 'No expire time given or set on construction.' );

		$number	= 0;
		$index	= new DirectoryIterator( $this->path );
		foreach( $index as $entry )
		{
			if( $entry->isDot() || $entry->isDir() )
				continue;
			$pathName	= $entry->getPathname();
			if( substr( $pathName, -7 ) !== ".serial" )
				continue;
			if( $this->isExpired( $pathName, $expires ) )
				$number	+= (int) @unlink( $pathName );
		}
		return $number;
	}
1338383
public function deleteExpired()
    {
        $cache = $this->loadCache();
        if (is_array($cache)) {
            $i = 0;
            $cache = array_map(function ($value) use (& $i) {
                if (!$this->isExpired($value['time'], $value['expire'])) {
                    ++$i;
                    return $value;
                }
            }, $cache);
            if ($i > 0) {
                $this->saveCache($cache);
            }
        }
        return $this;
    }
335385
protected function rollback(array $old, array $new)
    {
        foreach ($old as $key => $value) {
            $current = $this->cache->get($key, $token);

            /*
             * If the value right now equals the one we planned to write, it
             * should be restored to what it was before. If it's yet something
             * else, another process must've stored it and we should leave it
             * alone.
             */
            if ($current === $new) {
                /*
                 * CAS the rollback. If it fails, that means another process
                 * has stored in the meantime and we can just leave it alone.
                 * Note that we can't know the original expiration time!
                 */
                $this->cas($token, $key, $value, 0);
            }
        }

        $this->clear();
    }
1361817
public function expireAtDate($datetime, $write = true)
    {
        $this->ExpireAtDate = DBField::create_field('SS_Datetime', $datetime)->Format('Y-m-d H:i:s');

        if ($write) {
            $this->write();
        }

        return $this;
    }
355547
public function isExpired()
    {
        $ret = true;

        $now = new \DateTime();
        if ($this->expirationDate > $now) {
            $ret = false;
        }

        return $ret;
    }
238995
public function removeExpired(): int
    {
        $queryBuilder = $this->getEntityManager()->createQueryBuilder();
        $queryBuilder->delete(AdministratorToken::class, 'token')->where('token.expiry <= CURRENT_TIMESTAMP()');

        return (int)$queryBuilder->getQuery()->execute();
    }