"""
Scan through the Samples table for oldish entries and remove them.
"""
import logging
import json
import boto3
import time
import decimal
from boto3.dynamodb.conditions import Key, Attr

logger = logging.getLogger()
logger.setLevel(logging.ERROR)

def purge_item(item, batch):
    response = batch.delete_item(
        Key={
            'event' : item['event'],
            'id': item['id']
        }
    )

def purge_handler(event, context):
    dynamodb = boto3.resource('dynamodb')

    table = dynamodb.Table('Samples')
    filter = Attr('time').lte(decimal.Decimal(time.time()-172800))

    with table.batch_writer() as batch:
        response = table.scan(
            FilterExpression=filter
        )

        for item in response['Items']:
            purge_item(item, batch)

        while 'LastEvaluatedKey' in response:
            response = scan(
                FilterExpression=filter,
                ExclusiveStartKey=response['LastEvaluatedKey']
            )

            for item in response['Items']:
                purge_item(item, batch)

@@ -124,23 +124,76 @@
 ime%0A
+from decimal 
 import 
-d
+D
 ecimal%0A
+from boto3.dynamodb.types import Binary%0A
 from
@@ -304,35 +304,41 @@
 %0A%0Adef purge_
-item(i
+vote(vo
 te
-m
 , 
+votes_
 batch):%0A    
@@ -341,88 +341,479 @@
     
-response = batch.delete_item(%0A        Key=%7B%0A            'event' : item%5B'event'%5D,
+votes_batch.delete_item(Key=%7B'sample' : vote%5B'sample'%5D, 'user': vote%5B'user'%5D%7D)%0A%0Adef purge_sample(sample, vote_table, samples_batch, votes_batch):%0A    samples_batch.delete_item(Key=%7B'event' : sample%5B'event'%5D, 'id': sample%5B'id'%5D%7D)%0A    sample_identity =  Binary(sample%5B'id'%5D.value + bytearray(sample%5B'event'%5D, %22utf_8%22))%0A    sample_key = Key('sample').eq(sample_identity)%0A%0A    response = vote_table.query(KeyConditionExpression=sample_key)%0A%0A    for item in response%5B'Items'%5D:
 %0A   
@@ -821,44 +821,288 @@
     
-    'id': item%5B'id'%5D%0A        %7D%0A    )
+purge_vote(item, votes_batch)%0A%0A        while 'LastEvaluatedKey' in response:%0A            response = query(KeyConditionExpression=sample_key, ExclusiveStartKey=response%5B'LastEvaluatedKey'%5D)%0A%0A            for item in response%5B'Items'%5D:%0A                purge_vote(item, votes_batch)%0A%0A
 %0A%0Ade
@@ -1181,16 +1181,23 @@
 ')%0A%0A    
+sample_
 table = 
@@ -1222,16 +1222,58 @@
 mples')%0A
+    vote_table = dynamodb.Table('Votes')%0A%0A
     filt
@@ -1298,16 +1298,8 @@
 lte(
-decimal.
 Deci
@@ -1333,16 +1333,23 @@
    with 
+sample_
 table.ba
@@ -1364,16 +1364,66 @@
 er() as 
+samples_batch, vote_table.batch_writer() as votes_
 batch:%0A 
@@ -1440,16 +1440,23 @@
 ponse = 
+sample_
 table.sc
@@ -1559,35 +1559,70 @@
       purge_
-item(item, 
+sample(item, vote_table, samples_batch, votes_
 batch)%0A%0A    
@@ -1880,19 +1880,54 @@
 rge_
-item(item, 
+sample(item, vote_table, samples_batch, votes_
 batc

Purge the votes related to the samples that were already being purged.