<p>I have two deployments running in one namespace with name deployment-A and deployment-B.</p>

<p>Currently, it is configured in such a way that if A is running then my Jenkins job will deploy B and vice-versa. But the job fails when both A and B are running. 
Now I want it to modify a bit so that if both A and B are running then the job should delete the oldest one (based on their deployment time).</p>

<p>I know it can be achieved with some shell script but not able to figure out the exact command.</p>

vivek
<p>Here is command for you:</p>

<p><code>kubectl delete deployment $(kubectl get deployments --sort-by=.metadata.creationTimestamp -o json | jq -r .items[0].metadata.name)</code></p>

<p>Explanation: get all deployments sorted by timestamp, lower timestamp = older deployment, so first element is the oldest deployment. Than using json output and jq linux tool just get name of that deployment and pass it to delete deployment command. </p>
