i want keep the 100 rows that has the biggest timestamp and delete all the other rows... Here follow the code that i have create :

$values_to_delete = db_delete('table1', 't');
  $values_to_delete->range(101, 99999999);

1 Answer 1


The DeleteQuery class doesn't have orderBy or range functions, so I would imagine you would need to write your own DELETE with SELECT subquery. An example from Stack Overflow

  • could you help me to do this difficult thing?? I am new in drupal coding..
    – Karmen
    Apr 15, 2015 at 10:04
  • 2
    Drupal doesn't have its own version of SQL @Karmen, that's just a plain old SQL issue. The link in the answer should help you to get closer to what's required. If you have trouble, you can always ask SQL questions yourself on Stack Overflow. Just be sure to do due diligence beforehand - it's quite a lot stricter over there and questions without plenty of explanation tend to get downvoted/closed pretty quickly
    – Clive
    Apr 15, 2015 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.