diff --git a/models/repo.go b/models/repo.go
index 578b54f8a7..d72d3d1da2 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1504,14 +1504,6 @@ func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) {
 
 // DeleteRepository deletes a repository for a user or organization.
 func DeleteRepository(uid, repoID int64) error {
-	repo := &Repository{ID: repoID, OwnerID: uid}
-	has, err := x.Get(repo)
-	if err != nil {
-		return err
-	} else if !has {
-		return ErrRepoNotExist{repoID, uid, ""}
-	}
-
 	// In case is a organization.
 	org, err := GetUserByID(uid)
 	if err != nil {
@@ -1529,6 +1521,20 @@ func DeleteRepository(uid, repoID int64) error {
 		return err
 	}
 
+	repo := &Repository{ID: repoID, OwnerID: uid}
+	has, err := sess.Get(repo)
+	if err != nil {
+		return err
+	} else if !has {
+		return ErrRepoNotExist{repoID, uid, ""}
+	}
+
+	if cnt, err := sess.Id(repoID).Delete(&Repository{}); err != nil {
+		return err
+	} else if cnt != 1 {
+		return ErrRepoNotExist{repoID, uid, ""}
+	}
+
 	if org.IsOrganization() {
 		for _, t := range org.Teams {
 			if !t.hasRepository(sess, repoID) {
@@ -1540,7 +1546,6 @@ func DeleteRepository(uid, repoID int64) error {
 	}
 
 	if err = deleteBeans(sess,
-		&Repository{ID: repoID},
 		&Access{RepoID: repo.ID},
 		&Action{RepoID: repo.ID},
 		&Watch{RepoID: repoID},
@@ -1555,24 +1560,27 @@ func DeleteRepository(uid, repoID int64) error {
 	}
 
 	// Delete comments and attachments.
-	issues := make([]*Issue, 0, 25)
-	attachmentPaths := make([]string, 0, len(issues))
+	issueIDs := make([]int64, 0, 25)
+	attachmentPaths := make([]string, 0, len(issueIDs))
 	if err = sess.
+		Table("issue").
+		Cols("id").
 		Where("repo_id=?", repoID).
-		Find(&issues); err != nil {
+		Find(&issueIDs); err != nil {
 		return err
 	}
-	for i := range issues {
-		if _, err = sess.Delete(&Comment{IssueID: issues[i].ID}); err != nil {
+
+	if len(issueIDs) > 0 {
+		if _, err = sess.In("issue_id", issueIDs).Delete(&Comment{}); err != nil {
 			return err
 		}
-		if _, err = sess.Delete(&IssueUser{IssueID: issues[i].ID}); err != nil {
+		if _, err = sess.In("issue_id", issueIDs).Delete(&IssueUser{}); err != nil {
 			return err
 		}
 
 		attachments := make([]*Attachment, 0, 5)
 		if err = sess.
-			Where("issue_id=?", issues[i].ID).
+			In("issue_id=?", issueIDs).
 			Find(&attachments); err != nil {
 			return err
 		}
@@ -1580,13 +1588,13 @@ func DeleteRepository(uid, repoID int64) error {
 			attachmentPaths = append(attachmentPaths, attachments[j].LocalPath())
 		}
 
-		if _, err = sess.Delete(&Attachment{IssueID: issues[i].ID}); err != nil {
+		if _, err = sess.In("issue_id", issueIDs).Delete(&Attachment{}); err != nil {
 			return err
 		}
-	}
 
-	if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
-		return err
+		if _, err = sess.Delete(&Issue{RepoID: repoID}); err != nil {
+			return err
+		}
 	}
 
 	if _, err = sess.Where("repo_id = ?", repoID).Delete(new(RepoUnit)); err != nil {