<p>Was looking to find kubernetes pvc which are older than certain time limit and remove.
Couldn't find enough resource on web. Any idea?</p>
<p>Anybody knows what might be the right attribute?</p>
<p>This is the script version to list all pvc:</p>
<pre><code>from kubernetes import client, config

config.load_kube_config()

v1 = client.CoreV1Api()

pvcs = v1.list_persistent_volume_claim_for_all_namespaces(watch=False)
print(&quot;---- PVCs ---&quot;)
print(&quot;%-16s\t%-40s\t%-6s&quot; % (&quot;Name&quot;, &quot;Volume&quot;, &quot;Size&quot;))
for pvc in pvcs.items:
    print(&quot;%-16s\t%-40s\t%-6s&quot; %
            (pvc.metadata.name, pvc.spec.volume_name,    
            pvc.spec.resources.requests['storage']))
</code></pre>
<p>Edit:<br />
Solution for now:</p>
<pre><code>from kubernetes import client, config, watch
from datetime import datetime, timezone, timedelta
import os, subprocess

#----------------------------------------------------
now = datetime.utcnow()
only_date = now.date()
print (&quot; &quot;)
print (&quot;Today's date is:&quot;, (only_date))
print (&quot; &quot;)
retention_days = 0
#------------------------------------------------------

ns = os.getenv(&quot;K8S_NAMESPACE&quot;)
if ns is None:
    ns = &quot;foo&quot; # Namespace foo is aleady existing, or enter namespace

config.load_kube_config()

v1 = client.CoreV1Api()

pvcs = v1.list_namespaced_persistent_volume_claim(namespace=ns, watch=False)
print(&quot;---- PVCs ---&quot;)
print(&quot;%-16s\t%-40s\t%-6s\t%-6s&quot; % (&quot;Name&quot;, &quot;Volume&quot;, &quot;Size&quot;, &quot;Date_Created&quot;))
for pvc in pvcs.items:
    print(&quot;%-16s\t%-40s\t%-6s\t%-6s&quot; %
            (pvc.metadata.name, pvc.spec.volume_name,    
            pvc.spec.resources.requests['storage'], pvc.metadata.creation_timestamp.date()))
print(&quot;&quot;)
filtered_pvcs_date = pvc.metadata.creation_timestamp.date() 


if (only_date - filtered_pvcs_date) &gt; timedelta(retention_days):
    print (&quot;PVC is older than configured retention of %d days&quot; % (retention_days))
    
    cmd = ('kubectl delete pvc' + &quot; &quot; + pvc.metadata.name + &quot; &quot; + '-n' + &quot; &quot; + ns)
    os.system(cmd) 
    
    
else:
    print (&quot;PVC is younger than configured retention of %d days&quot; % (retention_days))
</code></pre>

<p>You can use the <a href="https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/object-meta/#Read-only" rel="nofollow noreferrer">metadata.creation_timestamp</a> field to check when an object was created:</p>
<pre class="lang-python prettyprint-override"><code>pvcs = v1.list_persistent_volume_claim_for_all_namespaces(watch=False)

#filter out all pvcs that have been created before 2021-04-25 18:25:30+00:00 (UTC)
from datetime import datetime, timezone
filtered_pvcs = [pvc for pvc in pvcs.items if pvc.metadata.creation_timestamp &gt; 
    datetime(2021, 4, 25, 18, 25, 30, 0, tzinfo=timezone.utc)]

for pvc in filtered_pvcs:
    print ...
</code></pre>
