morrislaptop / firestore-php

Firestore SDK for PHP without gRPC
MIT License
67 stars 31 forks source link

Delete Feature #18

Open ronlinet opened 4 years ago

ronlinet commented 4 years ago

Do you plan to include delete feature in the project ?

ronlinet commented 4 years ago

Speaking about it I have just tested the bellow snippet and it works. This one is deleting content older than 20 hours.

use Google\Cloud\Firestore\FirestoreClient;
use Kreait\Firebase\ServiceAccount;
use Morrislaptop\Firestore\Factory;

        $serviceAccount = ServiceAccount::fromJsonFile( storage_path()  . '/app/google-services.json');
        $firestore = (new Factory)
            ->withServiceAccount($serviceAccount)
            ->createFirestore();

        $documents = $firestore->collection('alerts')->documents(['pageSize' => 100]);

        foreach ($documents as $document) {

                $hourdiff = round((time() - strtotime($document['date']))/36000, 1);

            if( $hourdiff  > 20 ) //everything older than 20 hours will be deleted
            {
                    $document->reference()->delete();
                }

            }
marianoarga commented 4 years ago

Thanks @ronlinet

ronlinet commented 3 years ago

This one delete a collection document by id ..

            $collection     = $this->firestore->collection('collection_name')->document( (int) $document->id );
            $collection->delete();